import tkinter as tk
import datetime
import time
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.seconds_left = 0
self._timer_on = False
self.create_widgets()
def create_widgets(self):
self.button = tk.Button(self, text="開始", fg="red",command=self.start)
self.button.pack()
def start(self):
self.pop = tk.Toplevel()
self.label = tk.Label(self.pop, text="00:00:00")
self.label.pack()
self.start_button()
def countdown(self):
self.label['text'] = self.convert_seconds_left_to_time()
if self.seconds_left:
self.seconds_left -= 1
self._timer_on = self.after(1000, self.countdown)
else:
self._timer_on = False
def start_button(self):
self.seconds_left = 90
self.stop_timer()
self.countdown()
a = 0
while a <100:
a = a + 1
time.sleep(0.1)
def stop_timer(self):
if self._timer_on:
self.after_cancel(self._timer_on)
self._timer_on = False
def convert_seconds_left_to_time(self):
return datetime.timedelta(seconds=self.seconds_left)
root = tk.Tk()
app = Application(master=root)
app.mainloop()
小弟想請教的是
我toplevel明明放在前面
為什麼它會執行while的東西後 才跳出視窗倒數呢
而且整個程式會當掉 變成沒有回應