AttributeError: application instance has no attribute 'after'

AttributeError: application instance has no attribute 'after'

谁能告诉我为什么会出现以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1437, in __call__
    return self.func(*args)
  File "./transrecv.py", line 137, in Enter
    self.after(tTimer, self.Enter)
AttributeError: application instance has no attribute 'after'

我正在尝试调整 class 以便它可以根据用户输入的信息重复命令或只执行一次。

代码:

class application:

    def __init__(self,window):
        """Initialize the Application """
        self.IDbox = tk.Entry(window, width = 5)
        self.IDbox.pack(side="left", expand=True)
        self.IDbox.insert(0,"ID")

        self.msgTypeBox = tk.Entry(window)
        self.msgTypeBox.pack(side="left", expand=True)
        self.msgTypeBox.insert(0,"Message Type")

        self.canTypeBox = tk.Entry(window)
        self.canTypeBox.pack(side="left", expand=True)
        self.canTypeBox.insert(0,"Can Type")

        self.tData0Box = tk.Entry(window, width = 5)
        self.tData0Box.pack(side="left", expand=True)
        self.tData0Box.insert(0,"Data0")

        self.tData1Box = tk.Entry(window, width = 5)
        self.tData1Box.pack(side="left", expand=True)
        self.tData1Box.insert(0,"Data1")

        self.tData2Box = tk.Entry(window, width = 5)
        self.tData2Box.pack(side="left", expand=True)
        self.tData2Box.insert(0,"Data2")

        self.tData3Box = tk.Entry(window, width = 5)
        self.tData3Box.pack(side="left", expand=True)
        self.tData3Box.insert(0,"Data3")

        self.tData4Box = tk.Entry(window, width = 5)
        self.tData4Box.pack(side="left", expand=True)
        self.tData4Box.insert(0,"Data4")

        self.tData5Box = tk.Entry(window, width = 5)
        self.tData5Box.pack(side="left", expand=True)
        self.tData5Box.insert(0,"Data5")

        self.tData6Box = tk.Entry(window, width = 5)
        self.tData6Box.pack(side="left", expand=True)
        self.tData6Box.insert(0,"Data6")

        self.tData7Box = tk.Entry(window, width = 5)
        self.tData7Box.pack(side="left", expand=True)
        self.tData7Box.insert(0,"Data7")

        self.tTimerBox = tk.Entry(window, width = 5)
        self.tTimerBox.pack(side="left", expand=True)
        self.tTimerBox.insert(0,"0")

        self.TranButton = tk.Button(window,
                                    text="Transmit",
                                    command = self.Enter)
        self.TranButton.pack(side="bottom")

    def Enter(self):
        """ Someone Pressed Enter """
        canID = self.IDbox.get()
        msgType = self.msgTypeBox.get()
        canType = self.canTypeBox.get()
        DLC = 8
        tData0 = self.tData0Box.get()
        tData1 = self.tData1Box.get()
        tData2 = self.tData2Box.get()
        tData3 = self.tData3Box.get()
        tData4 = self.tData4Box.get()
        tData5 = self.tData5Box.get()
        tData6 = self.tData6Box.get()
        tData7 = self.tData7Box.get()
        tTimer = self.tTimerBox.get()
        print("You tranmitted >> %s %s %s %d %s %s %s %s %s %s %s %s " %
             (msgType, canType, canID, DLC,  tData0, tData1,
               tData2, tData3, tData4, tData5, tData6, tData7))
        if int(tTimer) <= 0:
            system('echo "%s %s 0x%s %d 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s" >/dev/pcan33' %
                    (msgType, canType, canID, DLC, tData0, tData1,
                     tData2, tData3, tData4, tData5, tData6, tData7))
        else:
            self.after(tTimer, self.Enter)

 root = tk.Tk()

 myapp = application(root)
 root.mainloop()

如错误所述,您的 class 没有属性 afterafter 是 Tkinter 小部件的一种方法,您的 class 不继承任何小部件。

我建议将您的代码改成这样:

class申请:

def __init__(self,window):
    ...
    self.window = window
    ...
def Enter(self):
    ...
    if int(tTimer) <= 0:
        ...
    else:
        self.window.after(tTimer, self.Enter)

另一个解决方案是创建您自己的 after 调用 self.window.after 的方法。