I want that when the "Start" button is pressed, it executes code and counts from 3 (3... 2... 1... Ready!), with delay between each and printing the numbers in the program window. I used tkinter. But I can't get the countdown to appear when I press the "Start" button. And I don't know how to add the code that is executed after pressing the button.
import tkinter as tk
from tkinter import ttk
import time
class Aplicacion:
def __init__(self,master):
self.master=master
self.master.title('Title')
self.master.geometry('290x50')
self.inicializar_gui()
def inicializar_gui(self):
lbl_bienvenido=tk.Label(self.master,text='Hi!',font=('Helvetica',10))
lbl_bienvenido.place(x=0,y=0)
btn_login=tk.Button(self.master,text='Start')
btn_login['command']=self.comando
btn_login.place(x=10,y=23)
def comando(self):
entry = ttk.Entry(state=tk.DISABLED, takefocus=False)
entry.place(x=63, y=25)
entry.insert(0, "1...")
# print("3")
# time.sleep(1)
# print("2")
# time.sleep(1)
# print("1")
# time.sleep(1)
# print("Ready!")
def main ():
root = tk.Tk()
ventana = Aplicacion(root)
root.mainloop()
if __name__ == "__main__":
main()
CodePudding user response:
here is an exemple that you could adjust to your need :
import tkinter
root = tkinter.Tk()
txt = tkinter.Text(root)
a = 4
def countdown():
global a
if a>0:
txt.delete('1.0','end')
a -=1
txt.insert('0.0',str(a))
root.after(1000,countdown)
if a == 0 :
txt.delete('1.0','end')
txt.insert('0.0','Ready!!')
btn = tkinter.Button(root,text = 'START',command = countdown)
txt.pack()
btn.pack()
root.mainloop()