Tkinter gui停止处理(二进制时钟)

时间:2014-02-02 23:51:16

标签: python tkinter

由于我是Python的新手,我希望这是一个非常明显的问题。

我正在编码二进制时钟(即显示1和0,但最终会显示大型LED的图形)

以下是我目前使用的代码:

#Simple binary clock
#Python 3.3.2

from tkinter import *
import time

root=Tk()
root.title("Title")
root.geometry("500x500")

def task():

    tme= time.strftime("%H",time.localtime()) + time.strftime("%M",time.localtime()) + time.strftime("%S",time.localtime())
    print(tme)
    hpos=0
    for c in tme:               #tme set to HHMMSS, iterate through each digit
        col=50+hpos*50          #Set column position
        b=format(int(c),'04b')  #Covert digit to 4 bit binary
        vpos=0

        for r in b:
            row=50+vpos*50
            if r=="1":
                label1=Label(root,text="1")                
            else:
                label1=Label(root,text="0")                
            label1.place(x=col,y=row)
            vpos+=1
        hpos+=1

    root.after(1000,task)       #reschedule event, 1000=1sec

root.after(100,task)
root.mainloop()

问题是这样 - 在离开运行代码大约15分钟之后,它会慢下来并且停止运转。我已经在不止一台PC上尝试过相同的效果,我希望这可以在Raspberry Pi上运行,但同样的结果也是如此。

我最终会让表单填满屏幕并在标签小部件中使用图形 - 我愿意以不同的方式解决解决方案。

提前感谢您提供的任何帮助。

JJ

2 个答案:

答案 0 :(得分:1)

label1 = Label(root, ...)每次都会创建一个新窗口,并将其放在上一个实例的顶部。所以随着时间的推移,你会创建越来越多的窗口,逐渐消耗你的记忆。

相反,创建一次标签并放置它。然后只需在任务中更新它的text属性,以便它在同一个窗口实例中显示新值。

此外,您可以在一次通话中time.strftime("%H%M%S",time.localtime())

格式化时间

示例

from Tkinter import *
import sys,time

class App():
    def __init__(self, parent):
        parent.title("Title")
        parent.geometry("500x500")
        self.labels = []
        self.parent = parent
        x,y = 50,50
        for index in range(3):
            label = Label(parent, text='0')
            label.place(x = x, y = y)
            self.labels.append(label)
            y += 50
        self.parent.after(1000, self.Task)

    def Task(self):
        t = time.strftime("%H:%M:%S", time.localtime())
        print(t)
        index = 0
        for c in t.split(':'):
            b = format(int(c), '04b')
            self.labels[index].configure(text=b)
            index += 1
        self.parent.after(1000, self.Task)


def main():
    root = Tk()
    app = App(root)
    root.mainloop()

if __name__=='__main__':
    sys.exit(main())

答案 1 :(得分:0)

我试图按照概述进行更改,我为每个位添加了一个单独的小部件,初始化它们,将它们放在表单上。

在更改窗口小部件的文本时,表单不会更新。我已经看过标签小部件是否有更新方法,但显然没有。我必须在这里遗漏一些非常明显的东西。

我在这里有一个精简版的代码,只显示秒的最后一位数,我错过了什么?:

#Simple binary clock

from tkinter import *
import time

root=Tk()
root.title("Title")
root.geometry("500x500")

def task():

    tme=time.strftime("%S",time.localtime())    #testing on just the seconds
    tme=tme[1]  #testing, just take last digit of seconds
    print(tme)

    for c in tme:               #tme set to HHMMSS, iterate through each digit
        b=format(int(c),'04b')  #Covert digit to 4 bit binary

        print(b)
        if b[0]=="1":
            label0=Label(root,text="1")                
        else:
            label0=Label(root,text="0")

        if b[1]=="1":
            label1=Label(root,text="1")                
        else:
            label1=Label(root,text="0")

        if b[2]=="1":
            label2=Label(root,text="1")                
        else:
            label2=Label(root,text="0")

        if b[3]=="1":
            label3=Label(root,text="1")

        else:
            label3=Label(root,text="0")


    root.after(1000,task)       #reschedule event, 1000=1sec

label0=Label(root, text="*")
label0.place(x=50,y=50)

label1=Label(root, text="*")
label1.place(x=50,y=100)

label2=Label(root, text="*")
label2.place(x=50, y=150)

label3=Label(root, text="*")
label3.place(x=50,y=200)


root.after(1000,task)
root.mainloop()