需要线程帮助

时间:2013-10-15 15:39:29

标签: python multithreading python-3.x

有关如何使此代码工作的任何想法? 我使用的是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()

错误与我在评论中所说的相同。

感谢任何帮助,谢谢。

1 个答案:

答案 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()