O
O
OgnevEgor2021-10-24 22:22:31
Python
OgnevEgor, 2021-10-24 22:22:31

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

1 answer(s)
Q
QPDEH, 2021-12-07
@QPDEH

Set at first somewhere near to a root = Tkvariable. Let it be . Then replace with:iterat = 0

global iterar
if iterat <= 30:
    win.after(10, open_window)
    iterat += 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question