2017-09-18 154 views
-2

我有这个代码显示一个页面,然后当按PAGE1按钮去其他框架。在这里,我想显示任何标签的时间,但我希望它自动更新。我不能牛逼管理自己做Tkinter更新变量实时

import Tkinter as tk 
from Tkinter import * 
import time 
import datetime 

def clock(): 
    time = datetime.datetime.now().strftime("Time: %H:%M:%S") 
    print time 

class MainApp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     container = tk.Frame(self) 
     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 (MainPage, page1, help): 
      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 
      frame.grid(row=0, column=0, columnspan=12,sticky="nsew") 
      frame.grid_columnconfigure(0, weight=1) 
      frame.grid_columnconfigure(1, weight=1) 
      frame.grid_columnconfigure(2, weight=1) 
      frame.grid_columnconfigure(3, weight=1) 
      frame.grid_columnconfigure(4, weight=1) 
      frame.grid_columnconfigure(5, weight=1) 
      frame.grid_columnconfigure(6, weight=1) 
      frame.grid_columnconfigure(7, weight=1) 
      frame.grid_columnconfigure(8, weight=1) 
      frame.grid_columnconfigure(9, weight=1) 
      frame.grid_columnconfigure(10, weight=1) 
      frame.grid_columnconfigure(11, weight=1) 

     self.show_frame("MainPage") 
     clock() 
    def show_frame(self, page_name): 
     frame = self.frames[page_name] 
     frame.tkraise() 


class MainPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     # line 0 
     label00 = tk.Label(self, text='00', height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=0, column=0, sticky='nsew', columnspan=12) 
     # line 1 
     label10 = tk.Label(self, text='10') 
     label10.configure(relief='raised') 
     label10.grid(row=1, column=0, sticky='nsew') 
     label11 = tk.Label(self, text='LOGO') 
     label11.configure(relief='raised') 
     label11.grid(row=1, column=1, sticky='nsew', columnspan=4, rowspan=2) 
     label12 = tk.Label(self, text='12') 
     label12.configure(relief='raised') 
     label12.grid(row=1, column=5, sticky='nsew') 
     label13 = tk.Label(self, text='TITLE') 
     label13.configure(relief='raised') 
     label13.grid(row=1, column=6, sticky='nsew', columnspan=5) 
     label14 = tk.Label(self, text='14') 
     label14.configure(relief='raised') 
     label14.grid(row=1, column=11, sticky='nsew') 
     # line 2 
     label20 = tk.Label(self, text='20') 
     label20.configure(relief='raised') 
     label20.grid(row=2, column=0, sticky='nsew') 
     label21 = tk.Label(self, text='21') 
     label21.configure(relief='raised') 
     label21.grid(row=2, column=5, sticky='nsew') 
     label22 = tk.Label(self, text='Desc') 
     label22.configure(relief='raised') 
     label22.grid(row=2, column=6, sticky='nsew', columnspan=5) 
     label23 = tk.Label(self, text='23') 
     label23.configure(relief='raised') 
     label23.grid(row=2, column=11, sticky='nsew') 
     # line 3 
     label30 = tk.Label(self, text='30', height=2) 
     label30.configure(relief='raised') 
     label30.grid(row=3, column=0, sticky='nsew', columnspan=12) 
     #line 4 
     label40 = tk.Label(self, text='40', width=10) 
     label40.configure(relief='raised') 
     label40.grid(row=4, column=0, sticky='nsew') 
     label41 = tk.Label(self, text='STATUS', font=("Helvetica", 16), justify='center', fg="blue") 
     label41.configure(relief='raised') 
     label41.grid(row=4, column=1, columnspan=10) 
     label42 = tk.Label(self, text='42', width=10) 
     label42.configure(relief='raised') 
     label42.grid(row=4, column=11, sticky='nsew') 
     #line 5 
     label50 = tk.Label(self, text='50', height=2) 
     label50.configure(relief='raised') 
     label50.grid(row=5, column=0, columnspan=12, sticky="nsew") 
     #line 6 
     label60 = tk.Label(self, text='60', height=2) 
     label60.configure(relief='raised') 
     label60.grid(row=6, column=0, sticky='nsew') 
     buttonauto = tk.Button(self, text="PAGE1", font=("Helvetica", 16), justify='center', width=40, height=5, 
           command=lambda: controller.show_frame("page1")) 
     buttonauto.grid(row=6, column=1, columnspan=4) 
     label61 = tk.Label(self, text='61', height=2) 
     label61.configure(relief='raised') 
     label61.grid(row=6, column=5, sticky='nsew') 
     label62 = tk.Label(self, text='62', height=2) 
     label62.configure(relief='raised') 
     label62.grid(row=6, column=6, sticky='nsew') 
     buttoncam = tk.Button(self, text="HELP",font=("Helvetica", 16), justify='center', width=40, height=5, 
           command=lambda: controller.show_frame("help")) 
     buttoncam.grid(row=6 , column=7, columnspan=4) 
     label63 = tk.Label(self, text='63', height=2) 
     label63.configure(relief='raised') 
     label63.grid(row=6, column=11, sticky='nsew') 
     # line 7 
     label70 = tk.Label(self, text='70', height=2) 
     label70.configure(relief='raised') 
     label70.grid(row=7, column=0, sticky='nsew', columnspan=12) 

     #line 13 
     label13 = tk.Label(self, text='', height=2) 
     label13.configure(relief='raised') 
     label13.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 14 
     label14 = tk.Label(self, text='', width=10) 
     label14.grid(row=14, column=0, sticky='w') 
     buttonhlp = tk.Button(self, text="Help", width=20, height = 3, 
           command=lambda: controller.show_frame("help")) 
     buttonhlp.grid(row=14, column=1, columnspan=4) 
     label14 = tk.Label(self, text='') 
     label14.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label14 = tk.Label(self, text='', width=10) 
     label14.grid(row=14, column=11, sticky='e') 
     #line 15 
     label15 = tk.Label(self, text='', height=5) 
     label15.grid(row=15, column=0, columnspan=12, sticky="nsew") 


class page1(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     #line 0 
     label00 = tk.Label(self, text='PAGE1', height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 1 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=0, sticky='w') 
     buttonback = tk.Button(self, text="Back", width=20, height = 3, 
           command=lambda: controller.show_frame("MainPage")) 
     buttonback.grid(row=14, column=1, columnspan=4) 
     label10= tk.Label(self, text='') 
     label10.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=11, sticky='e') 
     #line 2 
     label20 = tk.Label(self, text='', height=5) 
     label20.grid(row=15, column=0, columnspan=12, sticky="nsew") 

class help(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     #line 0 
     label00 = tk.Label(self, text='HELP', height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 1 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=0, sticky='w') 
     buttonback = tk.Button(self, text="Back", width=20, height = 3, 
           command=lambda: controller.show_frame("MainPage")) 
     buttonback.grid(row=14, column=1, columnspan=4) 
     label10= tk.Label(self, text='') 
     label10.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=11, sticky='e') 
     #line 2 
     label20 = tk.Label(self, text='', height=5) 
     label20.grid(row=15, column=0, columnspan=12, sticky="nsew") 


def close_window(): 
    app.destroy() 


if __name__ == "__main__": 
    app = MainApp() 

    app.overrideredirect(True) 
    app.geometry("{0}x{1}+0+0".format(app.winfo_screenwidth(), app.winfo_screenheight())) 
    app.focus_set() # <-- move focus to this widget 


    app.mainloop() 

所以其实我想,当打开第1页框,在label00我想看一次,我在代码的开始打印。

+0

你能提供一个最小代码来复制问题吗? – Fejs

+2

请将代码缩小为[mcve]。另外,你有没有做过任何研究?本网站上有许多关于用tkinter显示时间或时钟的问题。 –

+0

你是对的,将来我会提供最少的代码。 –

回答

0

一种方法是定期调用一个函数并更新 GUI。在Tkinter中,这通常通过使用通用小部件方法after来完成。

我修改您的代码来创建一个StringVar 属性持有的时间价值和使用after()调用一个新的clock()方法 这也被添加到您的MainApp类。所有这些方法都会更新 类实例的StringVar属性。

我做的另一件事是修改您的Page1 类的__init__()方法创建label00小部件时使用的textvariable选项,而不是text选项,并指定添加到MainApp类作为StringVar属性它的价值。

这样做的好处是只要变量的值发生变化,标签就会自动更新。另外,您可以根据需要从GUI的不同部分引用相同的属性。

注意我只修改了Page1类的label00属性...您需要做类似于其他想要显示当前时间的地方。 (他们都可以使用相同的StringVar属性。)

import time 
import datetime 
import Tkinter as tk 
from Tkinter import * 

class MainApp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.timevar = tk.StringVar() # Added. 

     self.frames = {} 
     for F in (MainPage, Page1, Help): 
      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 
      frame.grid(row=0, column=0, columnspan=12,sticky="nsew") 
      frame.grid_columnconfigure(0, weight=1) 
      frame.grid_columnconfigure(1, weight=1) 
      frame.grid_columnconfigure(2, weight=1) 
      frame.grid_columnconfigure(3, weight=1) 
      frame.grid_columnconfigure(4, weight=1) 
      frame.grid_columnconfigure(5, weight=1) 
      frame.grid_columnconfigure(6, weight=1) 
      frame.grid_columnconfigure(7, weight=1) 
      frame.grid_columnconfigure(8, weight=1) 
      frame.grid_columnconfigure(9, weight=1) 
      frame.grid_columnconfigure(10, weight=1) 
      frame.grid_columnconfigure(11, weight=1) 

     # Synchronize with the computer's clock. 
     snooze = (1000000 - datetime.datetime.now().microsecond)/1000000. 
     if snooze > 0: 
      time.sleep(snooze) # Sleep until next whole second. 
     self.clock() # Starts string variable update process. 

     self.show_frame("MainPage") 

    def show_frame(self, page_name): 
     frame = self.frames[page_name] 
     frame.tkraise() 

    # Additional method added. 
    def clock(self): 
     self.timevar.set(datetime.datetime.now().strftime("Time: %H:%M:%S")) 
     self.after(1000, self.clock) # Update every second (1000 millsecs) 


class MainPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     # line 0 
     label00 = tk.Label(self, text='00', height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=0, column=0, sticky='nsew', columnspan=12) 
     # line 1 
     label10 = tk.Label(self, text='10') 
     label10.configure(relief='raised') 
     label10.grid(row=1, column=0, sticky='nsew') 
     label11 = tk.Label(self, text='LOGO') 
     label11.configure(relief='raised') 
     label11.grid(row=1, column=1, sticky='nsew', columnspan=4, rowspan=2) 
     label12 = tk.Label(self, text='12') 
     label12.configure(relief='raised') 
     label12.grid(row=1, column=5, sticky='nsew') 
     label13 = tk.Label(self, text='TITLE') 
     label13.configure(relief='raised') 
     label13.grid(row=1, column=6, sticky='nsew', columnspan=5) 
     label14 = tk.Label(self, text='14') 
     label14.configure(relief='raised') 
     label14.grid(row=1, column=11, sticky='nsew') 
     # line 2 
     label20 = tk.Label(self, text='20') 
     label20.configure(relief='raised') 
     label20.grid(row=2, column=0, sticky='nsew') 
     label21 = tk.Label(self, text='21') 
     label21.configure(relief='raised') 
     label21.grid(row=2, column=5, sticky='nsew') 
     label22 = tk.Label(self, text='Desc') 
     label22.configure(relief='raised') 
     label22.grid(row=2, column=6, sticky='nsew', columnspan=5) 
     label23 = tk.Label(self, text='23') 
     label23.configure(relief='raised') 
     label23.grid(row=2, column=11, sticky='nsew') 
     # line 3 
     label30 = tk.Label(self, text='30', height=2) 
     label30.configure(relief='raised') 
     label30.grid(row=3, column=0, sticky='nsew', columnspan=12) 
     #line 4 
     label40 = tk.Label(self, text='40', width=10) 
     label40.configure(relief='raised') 
     label40.grid(row=4, column=0, sticky='nsew') 
     label41 = tk.Label(self, text='STATUS', font=("Helvetica", 16), justify='center', fg="blue") 
     label41.configure(relief='raised') 
     label41.grid(row=4, column=1, columnspan=10) 
     label42 = tk.Label(self, text='42', width=10) 
     label42.configure(relief='raised') 
     label42.grid(row=4, column=11, sticky='nsew') 
     #line 5 
     label50 = tk.Label(self, text='50', height=2) 
     label50.configure(relief='raised') 
     label50.grid(row=5, column=0, columnspan=12, sticky="nsew") 
     #line 6 
     label60 = tk.Label(self, text='60', height=2) 
     label60.configure(relief='raised') 
     label60.grid(row=6, column=0, sticky='nsew') 
     buttonauto = tk.Button(self, text="PAGE1", font=("Helvetica", 16), justify='center', width=40, height=5, 
           command=lambda: controller.show_frame("Page1")) 
     buttonauto.grid(row=6, column=1, columnspan=4) 
     label61 = tk.Label(self, text='61', height=2) 
     label61.configure(relief='raised') 
     label61.grid(row=6, column=5, sticky='nsew') 
     label62 = tk.Label(self, text='62', height=2) 
     label62.configure(relief='raised') 
     label62.grid(row=6, column=6, sticky='nsew') 
     buttoncam = tk.Button(self, text="HELP",font=("Helvetica", 16), justify='center', width=40, height=5, 
           command=lambda: controller.show_frame("Help")) 
     buttoncam.grid(row=6 , column=7, columnspan=4) 
     label63 = tk.Label(self, text='63', height=2) 
     label63.configure(relief='raised') 
     label63.grid(row=6, column=11, sticky='nsew') 
     # line 7 
     label70 = tk.Label(self, text='70', height=2) 
     label70.configure(relief='raised') 
     label70.grid(row=7, column=0, sticky='nsew', columnspan=12) 

     #line 13 
     label13 = tk.Label(self, text='', height=2) 
     label13.configure(relief='raised') 
     label13.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 14 
     label14 = tk.Label(self, text='', width=10) 
     label14.grid(row=14, column=0, sticky='w') 
     buttonhlp = tk.Button(self, text="Help", width=20, height = 3, 
           command=lambda: controller.show_frame("Help")) 
     buttonhlp.grid(row=14, column=1, columnspan=4) 
     label14 = tk.Label(self, text='') 
     label14.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label14 = tk.Label(self, text='', width=10) 
     label14.grid(row=14, column=11, sticky='e') 
     #line 15 
     label15 = tk.Label(self, text='', height=5) 
     label15.grid(row=15, column=0, columnspan=12, sticky="nsew") 


class Page1(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     #line 0 
#  label00 = tk.Label(self, text='PAGE1', height=2) 
     label00 = tk.Label(self, textvariable=controller.timevar, height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 1 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=0, sticky='w') 
     buttonback = tk.Button(self, text="Back", width=20, height = 3, 
           command=lambda: controller.show_frame("MainPage")) 
     buttonback.grid(row=14, column=1, columnspan=4) 
     label10= tk.Label(self, text='') 
     label10.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=11, sticky='e') 
     #line 2 
     label20 = tk.Label(self, text='', height=5) 
     label20.grid(row=15, column=0, columnspan=12, sticky="nsew") 

class Help(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     #line 0 
     label00 = tk.Label(self, text='HELP', height=2) 
     label00.configure(relief='raised') 
     label00.grid(row=13, column=0, columnspan=12, sticky="nsew") 
     #line 1 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=0, sticky='w') 
     buttonback = tk.Button(self, text="Back", width=20, height = 3, 
           command=lambda: controller.show_frame("MainPage")) 
     buttonback.grid(row=14, column=1, columnspan=4) 
     label10= tk.Label(self, text='') 
     label10.grid(row=14, column=5, columnspan=2) 
     buttonquit = tk.Button(self, text="Quit", width=20, height = 3, command=close_window) 
     buttonquit.grid(row=14, column=7, columnspan=4) 
     label10 = tk.Label(self, text='', width=10) 
     label10.grid(row=14, column=11, sticky='e') 
     #line 2 
     label20 = tk.Label(self, text='', height=5) 
     label20.grid(row=15, column=0, columnspan=12, sticky="nsew") 


def close_window(): 
    app.destroy() 


if __name__ == "__main__": 
    app = MainApp() 

    app.overrideredirect(True) 
    app.geometry("{0}x{1}+0+0".format(app.winfo_screenwidth(), app.winfo_screenheight())) 
    app.focus_set() # <-- move focus to this widget 


    app.mainloop() 
+0

如果该值每秒只更改一次,为什么要每秒调用该函数四次? –

+0

@Bryan:因为更新速度应该大于每秒一次,因为它不同步发生在一分钟之内。不过,假设它可能是。大概每半秒钟就能得到一次......但多一点必要的可能不会伤害太多。海事组织你应该坚持更严重的问题,人们在他们的tkinter代码(只是我的意见)。 – martineau

+0

如果这次点击被用于计时奥运比赛,亚秒级的准确性可能很重要。对于其他任何东西,它只是浪费CPU周期。 –

1

您应该使用after方法,以便定期更新显示时间的标签。 以下文章提供了有关如何操作的深入说明:How to use the after method to make a callback run periodically?

让我们来构建一个最简单的示例。 首先,我需要一个根,和一个标签:

import tkinter as tk 
root = tk.Tk() 
label = tk.Label(root, text="placeholder") 
label.pack() 

现在,我需要将设置当前时间标签的功能。 正如我链接到的Q/A中所解释的,我需要这个函数来调用它自己。 因为我想尽快刷新标签,我会要求函数在一毫秒后自动调用。 可使用time模块,但datetime模块包装后者以提供人类可读的时间。

import datetime 
def set_label(): 
    currentTime = datetime.datetime.now() 
    label['text'] = currentTime 
    root.after(1, set_label) 

现在,我会打电话给set_label功能,以便开始循环,然后调用root.mainloop以便进入应用程序的循环:

set_label() 
root.mainloop() 

一起:

import tkinter as tk 
import time 

def set_label(): 
    currentTime = datetime.datetime.now() 
    label['text'] = currentTime 
    root.after(1, set_label) 

root = tk.Tk() 
label = tk.Label(root, text="placeholder") 
label.pack() 

set_label() 
root.mainloop() 
+0

当你知道显示的值每秒只会改变一次时,为什么每秒要调用你的函数1000次? –

+0

@BryanOakley这并不完全正确,因为'datetime.now()'提供的值会下降到微秒。不过,我不得不承认,这提出了一个有效的问题,是否适合更新标签的频率超过每秒一次。我想这一切都取决于用户的需求。 –