Python Tkinter gui停止处理(二进制时钟)
由于我是Python新手,我希望这是一个显而易见的问题 我正在编写一个二进制时钟(即显示1和0,但最终将显示大型LED的图形) 以下是我迄今为止使用的代码:Python Tkinter gui停止处理(二进制时钟),python,tkinter,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",tim
#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分钟后,它会减速并停止运行。我在不止一台电脑上尝试过同样的效果,我想在覆盆子圆周率上使用它,但同样的结果
我最终会让表单填满屏幕,并在标签小部件中使用图形——我愿意接受以不同方式解决方案的建议
提前感谢您提供的任何帮助
JJ行
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())
我尝试按照概述的方式进行更改,我为每个位添加了一个单独的小部件,初始化它们,并将它们放在表单上 更改小部件的文本时,表单不会更新。我已经查看了标签小部件是否有更新方法,但显然没有。我一定错过了一些很明显的东西 我这里有一个精简版的代码,只显示秒的最后一位,我错过了什么
#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()
谢谢你。。。。只是想澄清一下。。。有没有可能像上面那样在一个循环中创建单独的标签小部件,或者我需要分别创建它们,并为每个小部件提供坐标(我希望避免这样做:-<)jj您需要将此视为创建窗口,然后更新它们。我已经用一个例子更新了答案,这个例子在一个类中构造UI,然后只更新计时器调用的任务方法中的文本字段。我已经粘贴了代码,它可以正常工作,应该不会花太长时间来调整它。谢谢你的帮助-我从这个小项目中学到了很多。