[英]Update/renew countdown frame whenever it is raised by tkraise() in tkinter
我正在嘗試創建一個帶有多個窗口的2分鍾倒計時應用程序。 我將所有窗口(框架)放入主容器中,然后每當單擊特定窗口的導航按鈕時,使用tkraise()來升高框架(例如:如果“返回起始頁面”按鈕是點擊)。 僅當首次創建PracticePage類的對象時,下面的代碼才能正常工作。 但是,當我從倒數計時器框架(類PracticePage)導航到另一個頁面時,計時器仍在后面運行。 換句話說,每當我從另一頁導航回到倒數計時器頁面時,計時器都不會從2分鍾開始倒數。 我希望每當定時器框架升高時,它就從2分鍾開始倒計時。 我是編程的初學者。 如果我的問題和代碼令人困惑,我深表歉意。 有人可以幫忙嗎? 先感謝您。
下面是我的代碼:
import tkinter as tk
from tkinter import *
from tkinter import ttk
import time
class App(tk.Tk): #we want this class to inherit from tk.Tk
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self,"PreSys")
container = tk.Frame(self, height = 1000, width =1000)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (SignInPage, StartPage, PracticePage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(SignInPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise() #to raise one of the frames up to the front
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.startButton = ttk.Button(self, text="スタート", command = lambda: controller.show_frame(PracticePage))
self.startButton.pack()
class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
self.backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
self.homeButton = ttk.Button(self, text="サインアウト", command = lambda: controller.show_frame(SignInPage))
self.timeLeft.pack()
self.backButton.pack()
self.homeButton.pack()
#rc.start_record(SignInPage.entry_name.get())
self.remaining = 0
self.countdown(121)
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.timeLeft.configure(text="お疲れ様です!")
else:
mins, secs = divmod(self.remaining,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.configure(text=str(mins) +"分"+ str(secs) +"秒")
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
apps = App()
apps.mainloop()
在我們提供解決方案之前,請確保在提供您的代碼后它可以工作。 您刪除了SignInPage
,因為它是沒有必要的,但你沒有跟進,並刪除它(即-其他引用self.show_frame(SignInPage)
在App.__init__
和self.homebutton
在PracticePage
。
至於您重置計時器的問題,有很多方法可以解決,但它們全都歸結為您正在做的所有事情都是提高幀數:您不包含任何會影響countdown
的代碼目前正在切換到。
由於這是一個相對簡單的應用程序,因此我將建議最簡單的解決方案:當您提高PracticePage
時,請重置其remaining
變量。 為了使它盡可能簡單和一致,我們只需要修改show_frame
## You should store variables that are meaningful (you can place this underneath the imports)
PRACTICETIME = 121
## App():
def show_frame(self, cont):
frame = self.frames[cont]
if cont == PracticePage:
frame.remaining = PRACTICETIME
frame.tkraise() #to raise one of the frames up to the front
在較大的應用程序中,我建議您使用widget.after_cancel()
完全停止倒計時,或者在合理的情況下直接破壞並重新創建您的Pages
。 使用此方法,您可能會注意到一個1秒鍾的延遲,此時PracticePage仍顯示以前的remaining
時間:如果這確實讓您感到困擾,那么我建議您提取將PracticePage.timeLeft
格式化為自己的函數的代碼,然后從中調用該函數設置remaining
App.show_frame
后App.show_frame
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.