How do I make btn01 or btn go to a random spot every 4 milliseconds and when you click on it it will stop and do something else?

I want to make any btn go to a random spot every 3 milisecounds, and when you click the random button it will do something else like print Hi and stop the button from moving. Here is the code:

I tried while a == True: but it keeps frezzing when i press the "To make a video" button and just frezzes for a while

import time
import os
import tkinter as tk
import random
from tkinter import *
from tkinter import messagebox
from tkinter import Button
import math
from tkinter import Text
from tkinter import Grid
from tkinter import Place
#from tkinter import place
window = tk.Tk()
window.title("There is no game")
numberx = random.randint(1,200)
numbery = random.randint(1,200)
##def clickedrandom():
##  a = False
def toplayagame():
a = True
def tomakeavideo():
  T.delete('1.0', END)
  T.insert(tk.END, "People who are watching go hit that subscribe button and hit that like button also hit that little bell to turn on notifcations")
  T.configure(width = 25, height=6)
  while a == True:
    numberx = random.randint(1,200)
    numbery = random.randint(1,200)
    time.sleep(0.3) = numberx, y = numbery)

def pressed():
  T.delete('1.0', END)
  T.insert(tk.END, "Why are you here?"), y=200)
  T.configure(width = 17, height=1)
  btn.configure(text = "To play a game", width=12,command=toplayagame)
  btn1= Button(window, bd=10,text="To make a video",activebackground='White',activeforeground='Black',bg='Grey',fg='White',height=1,width=15,state=ACTIVE,command=tomakeavideo)

T = tk.Text(window, height=1, width=10)
T.insert(tk.END, "Hello user")

btn = Button(window, bd=10,text="Hello",activebackground='Black',activeforeground='White',bg='Grey',fg='White',height=1,width=4,state=ACTIVE,command=pressed)
btn.pack(), y=200)


1 answer

  • answered 2021-02-22 22:43 TheLizzard

    Use the <tkinter widget>.after(<time>, <function>) like this:

    import tkinter as tk
    import random
    playing_game = False
    def to_make_video():
        global btn1
        msg = "People who are watching go hit that subscribe button and"+\
              " hit that like button also hit that little bell to turn on"+\
              " notifcations"
        text_widget.delete("0.0", "end")
        text_widget.insert("end", msg)
        text_widget.configure(width=25, height=6)
    def game_won():
        # When the button is pressed:
        global playing_game
        playing_game = False
        text_widget.delete("0.0", "end")
        text_widget.insert("end", "Why aren't you leaving?")
        text_widget.configure(width=23, height=1)
    def move_button():
        global playing_game
        # If the game is over stop moving the button
        if not playing_game:
            return None
        # Pick the next random position for the button
        numberx = random.randint(1, 200)
        numbery = random.randint(1, 200), y=numbery)
        # After 500 milliseconds call `move_button` again
        # You can change the value to make it faster/slower
        root.after(500, move_button)
    def start_game():
        # Start the game
        global playing_game
        playing_game = True
        # Start the loop that keeps moving it to new random positions
    def pressed():
        global btn1
        # Ask the user why they are here
        text_widget.delete("0.0", "end")
        text_widget.insert("end", "Why are you here?")
   , y=200)
        btn.configure(text="To play a game", width=12, command=lambda: None)
        btn1 = tk.Button(root, bd=10, text="To make a video", bg="grey", fg="white",
                         activebackground="white", activeforeground="black",
                         height=1, width=15, command=to_make_video), y=200)
    # Create a window
    root = tk.Tk()
    text_widget = tk.Text(root, height=1, width=10)
    text_widget.insert(tk.END, "Hello user")
    btn = tk.Button(root, bd=10, text="Hello", activebackground="black",
                    activeforeground="white", bg="grey", fg="white", height=1,
                    width=4, command=pressed), y=200)
    # Run tkinter's mainloop