-1

关于如何让这段代码工作的任何想法?我正在使用 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()

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

感谢您提供任何帮助,谢谢。

4

1 回答 1

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()
于 2013-10-15T15:49:54.227 回答