有关如何使此代码工作的任何想法? 我使用的是Windows 7和python 3.3.2
from tkinter import *
class Clock:
def _init_(self, parent):
self.parent = parent
self.x = 0
self.y = 0
self.second = Label(parent)
self.minute = Label(parent)
self.second.pack()
self.minute.pack()
def advance(self):
self.second.config(text = "{0.x} second(s)".format(self))
self.minute.config(text = "{0.z} minute(s) has past".format(self))
self.x += 1
if self.x == 60:
self.x = 0
self.z += 1
self.parent.after(1000, self.advance)
mGui = Tk()
mGui.geometry("300x200+100+100")
mGui.title("Jono's Clock")
clock = Clock(mGui)
menubar = Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="Clock", command = clock.advance)
menubar.add_cascade(label="File", menu=filemenu)
mGui.config(menu = menubar)
mGui.mainloop()
错误与我在评论中所说的相同。
感谢任何帮助,谢谢。
答案 0 :(得分:1)
这里你不需要线程。如果您想要定期执行某些操作,请使用after
功能。
from tkinter import *
class Clock:
def __init__(self, parent):
self.parent = parent
self.x = 0
self.z = 0
self.second = Label(parent)
self.minute = Label(parent)
self.second.pack()
self.minute.pack()
def advance(self):
self.second.config(text='{0.x} second(s)'.format(self))
self.minute.config(text='{0.z} minute(s) past.'.format(self))
self.x += 1
if self.x == 60:
self.x = 0
self.z += 1
self.parent.after(1000, self.advance)
# This method is called after 1000 ms (1 second)
mGui = Tk()
mGui.geometry("300x200+100+100")
mGui.title("Jono's Clock")
clock = Clock(mGui)
menubar = Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="Clock", command=clock.advance)
menubar.add_cascade(label="File", menu=filemenu)
mGui.config(menu = menubar)
mGui.mainloop()