Answer the question
In order to leave comments, you need to log in
Python tkinter how to make after only fire a few times?
Hello
I am creating a small program. When you click on "NO", the monkey window should open a large number of times, I was able to do it, but the problem is that after compiling the program and running it for other users, after a certain number of times the picture appears, the program freezes and closes. I want to make the "open_window()" function work only a certain number of times, for example 30, but I can't create a counter
Code:
from tkinter import *
import random
def move(event):
x = random.random()
y = random.random()
global button2
button2.place(relx = x, rely = y)
def open_window():
win = Toplevel()
win.title("MONKEY"
win.geometry("1280x720")
monkey = Canvas(win, height=720, width=1280)
monkey.grid(row=0, column=0)
img = PhotoImage(file="monkey.png")
monkey.create_image( 50, 50, anchor="nw", image=img)
win.lift()
win.attributes('-topmost', True)
win.overrideredirect(True)
win.after(10, open_window)
monkey.mainloop()
root = Tk()
root.title("IMPORTANT QUESTION!")
root.geometry("600x250+650+400")
root.overrideredirect(True)
root.lift()
root.attributes('-topmost', True)
label = Label(root, text="Monkey?", font=('calibri', 30, "bold"),foreground='black', )
label.pack()
button1 = Button(root, text="No", font=('calibri', 20, "bold"), foreground='blue', command=open_window)
button1.place(relx=0.6, rely=0.5, anchor= 'center')
button2 = Button(root, font=('calibri', 20, "bold"), foreground='brown', text="Yes", command=move)
button2.place(relx=0.4, rely= 0.5, anchor='center')
button2.bind("", move)
root.mainloop()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question