【问题标题】:Python Clock (Simple Mistake?)Python 时钟(简单的错误?)
【发布时间】:2016-02-20 20:16:14
【问题描述】:

您好,我不知道下一步该做什么。 我希望警报标签在 Tkinter 小部件打开后更新时间。 任何想法都值得赞赏。我在网上查看了线程,但他们似乎以完全不同的方式来做这件事,我相信我有一种简单的方法可以让这个时钟正常工作。

from Tkinter import *
import time

#Window
alarm_window = Tk()

#Title
alarm_window.title('Alarm')

def off_press():
    alarm_window.destroy()

#Alarm Class
class Clock(object): 
    def __init__ (self, time, sleep):
        self.time =  time
        self.sleep = sleep 
        print "The time and date is %s" % (self.time) #temporary to see if it's working

        #Alarm
        alarm_label = Label(alarm_window, text = self.time)
        alarm_label.grid (row = 0, column = 1)

def refresh_time():
        for each in range(2): #only temporary until I find a way to make the label update
            alarm = Clock(time.ctime(), time.sleep(1))

#Off Button
off = Button(alarm_window, text = "Off", command = off_press)
off.grid (row = 1, column = 2)

#Snooze
snooze = Button(alarm_window, text = "Snooze")
snooze.grid (row= 1, column = 0)

#Run Program
refresh_time()

alarm_window.mainloop()

【问题讨论】:

    标签: python tkinter label clock


    【解决方案1】:

    您可以使用Tk().after(1000, refresh_time) 每秒更新时间。
    不要创建新标签来显示时间,而是使用label.configure(text=) 来更改标签文本。

    我做了一些修改来显示一个采样时钟:

    from Tkinter import *
    import time
    
    #Window
    alarm_window = Tk()
    
    #Title
    alarm_window.title('Alarm')
    
    def off_press():
        alarm_window.destroy()
    
    #Alarm Class
    class Clock(object): 
        def __init__ (self, time):
            self.time =  time
    
            print "The time and date is %s" % (self.time) #temporary to see if it's working
    
            #Alarm
            # Create instance variable to use it in "refresh_time"
            self.alarm_label = Label(alarm_window, text = self.time)
            self.alarm_label.grid (row = 0, column = 1)
    
    def refresh_time():
            #for each in range(2): #only temporary until I find a way to make the label update
    
            #Change label text with "configure(text=)" method
            clock.alarm_label.configure(text=time.ctime())
            #Use recursivity to make call after 1 second
            alarm_window.after(1000, refresh_time)
    
    
    #Off Button
    off = Button(alarm_window, text = "Off", command = off_press)
    off.grid (row = 1, column = 2)
    
    #Snooze
    snooze = Button(alarm_window, text = "Snooze")
    snooze.grid (row= 1, column = 0)
    
    #Run Program
    #refresh_time()
    
    #Create "clock" label
    clock = Clock(time.ctime())
    
    #Run "refresh_time" after 1 second
    alarm_window.after(1000, refresh_time)
    alarm_window.mainloop() 
    

    【讨论】:

    • 感谢您的回复!我在主循环之前添加了这一行,我试图让标签更新。小部件现在启动后,打印语句继续工作。我觉得我在做一些非常愚蠢的事情哈哈
    • @Mel 您正在使用refresh_time 方法创建标签,最好使用label.configure(text=new_time)
    • after(1, ...) 每毫秒更新一次,而不是每秒更新一次。除非您将时间显示为毫秒精度,否则您应该将该值设置为 CPU 密集度较低的值,例如 1000 (1000ms == 1 秒)
    • 感谢您的所有帮助 :) 我能够通过重新开始来解决它,我想我只是写过头了。
    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2016-05-16
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多