自我没有定义?

时间:2016-10-13 03:21:54

标签: python python-2.7 tkinter

我正在尝试将一个基本的学习时钟作为Tkinter的学习练习,但是在尝试运行时我得到一个错误,说自己没有被定义。这是错误消息。

Traceback (most recent call last):
    File "productivityclock.py", line 6, in <module>
        class gui(object):
    File "productivityclock.py", line 113, in gui
        while 0 < self.z:
NameError: name 'self' is not defined

这是我的代码,如果有人可以帮助我,我将非常感谢,谢谢

from Tkinter import *
import time

root = Tk()

class gui(object):
def __init__(self, master):

    self.frame = Frame(master)
    self.frame.pack()

    t_int = 0
    b_int = 0
    reps = 0

    self.menu()

    def menu(self): 
        self.button1 = Button(
        self.frame, text="Set Time", command = self.set_time)
        self.button2 = Button(
        self.frame, text="Set Breaks", command = self.set_break)
        self.button3 = Button(
        self.frame, text="Set Intervals", command = self.set_reps)
        self.button4 = Button(
        self.frame, text="Start", command = self.timer)
        self.button1.pack(side = LEFT)
        self.button2.pack(side = LEFT)
        self.button3.pack(side = LEFT)
        self.button4.pack(side = LEFT)

    def set_time(self):
        self.button1.pack_forget()
        self.button2.pack_forget()
        self.button3.pack_forget()
        self.button4.pack_forget()

        self.l = Label(self.frame, text = "Enter the time of each study session (minutes)")
        self.button = Button(
        self.frame, text="Get", command=self.on_button1)
        self.entry = Entry(self.frame)
        self.button.pack(side = RIGHT)
        self.entry.pack(side = LEFT)
        self.l.pack(side = LEFT)

    def set_break(self):
        self.button1.pack_forget()
        self.button2.pack_forget()
        self.button3.pack_forget()
        self.button4.pack_forget()

        self.l = Label(self.frame, text = "Enter the time of each break (minutes)")
        self.button = Button(
        self.frame, text="Get", command=self.on_button2)
        self.entry = Entry(self.frame)
        self.button.pack(side = RIGHT)
        self.entry.pack(side = LEFT)
        self.l.pack(side = LEFT)

    def set_reps(self):
        self.button1.pack_forget()
        self.button2.pack_forget()
        self.button3.pack_forget()
        self.button4.pack_forget()

        self.l = Label(self.frame, text = "Enter the amount of study sessions (minutes)")
        self.button = Button(
        self.frame, text="Get", command=self.on_button3)
        self.entry = Entry(self.frame)
        self.button.pack(side = RIGHT)
        self.entry.pack(side = LEFT)
        self.l.pack(side = LEFT)

    def on_button1(self):
        x = self.entry.get()
        self.t_int = x
        print self.t_int

        self.button.pack_forget()
        self.entry.pack_forget()
        self.l.pack_forget()

        self.menu()

    def on_button2(self):
        x = self.entry.get()
        self.b_int = x
        print self.b_int

        self.button.pack_forget()
        self.entry.pack_forget()
        self.l.pack_forget()

        self.menu()

    def on_button3(self):
         x = self.entry.get()
         self.reps = x
         print self.reps

         self.button.pack_forget()
         self.entry.pack_forget()
         self.l.pack_forget()

         self.menu()

    def timer(self):
         x = self.t_int
         y = self.b_int
         self.z = self.reps


         while 0 < self.z:
             while x > 0:
                 time.sleep(60)
                 n = Label(self.frame, text = "You have %r minutes left in your session") % x
                 n.pack(side = LEFT)
                 x = x - 1
             n.pack_forget
             while y > 0:
                 time.sleep(60)
                 self.e = Label(self.frame, text = "You have %r minutes left in your break") % y
                 self.e.pack(side = LEFT)
                 self.y = self.y - 1
             self.e.pack_forget()

             z = z - 1
             x = self.t_int
             y = self.b_int


app = gui(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

是的,在第113行,while 0 < self.z:没有正确缩进,以及它下面的所有行。