【问题标题】:AttributeError: 'int' object has no attribute 'tk'AttributeError:“int”对象没有属性“tk”
【发布时间】:2019-05-30 09:44:17
【问题描述】:

我实际上是在尝试不断更新嵌入在 tkinter GUI 窗口中的图形,并使用 after 函数在指定时间(10 毫秒)后调用更新函数。但是,在执行回调时出现上述错误,而且我对 tkinter 和 Python OOP 还是很陌生,所以我很可能会犯一个基本错误。这是代码(在 def update_plot 中发生错误):

class PageThree(tk.Frame):    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Graph Page!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to Home",
                             command=lambda: controller.show_frame(StartPage))
        button1.pack()
        global f, a
        f = Figure(figsize=(5,5), dpi=100)
        a = f.add_subplot(111)

        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        readbutton = ttk.Button(self, text="Start Data Collection", command=self.read_data)
        readbutton.pack()

        stopbutton = ttk.Button(self, text="Stop Data Collection",
                     command=self.stop_data)
        stopbutton.pack()

    def read_data(self):
        t_axis = []
        global starttime
        starttime = time()
        self.update_plot()

    def update_plot(self):
        global func_id
        t_axis.append(time()-starttime)
        output.append(random.random())
        a.cla()
        a.plot(t_axis, output)
        func_id = tk.Tk.after(samplerate, self.update_plot) # this is where it runs into an error

    def stop_data(self):
        global func_id
        tk.Tk.after_cancel(func_id)

完整的回溯错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/.../anaconda/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
    return self.func(*args)
  File "./gui2.py", line 202, in read_data
    self.update_plot()
  File "./gui2.py", line 211, in update_plot
    func_id = tk.Tk.after(samplerate, self.update_plot)
  File "/Users/.../anaconda/lib/python3.5/tkinter/__init__.py", line 592, in after
    self.tk.call('after', ms)
AttributeError: 'int' object has no attribute 'tk'

我对这个问题感到很困惑,并且在其他地方没有发现任何类似的问题。有什么指点吗?

【问题讨论】:

  • 错误在哪一行被抛出?编辑:刚刚看到你的编辑
  • 请提供完整的回溯错误。
  • Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/Users/.../anaconda/lib/python3.5/tkinter/__init__.py”,第 1550 行,在 call return self.func(*args) File "./gui2.py", line 202, in read_data self.update_plot() File "./gui2.py", line 211, in update_plot func_id = tk .Tk.after(samplerate, self.update_plot) 文件“/Users/.../anaconda/lib/python3.5/tkinter/__init__.py”,第 592 行,在 self.tk.call('after', ms) AttributeError: 'int' 对象没有属性 'tk'
  • 在你的问题中不是 cmets 哈哈。
  • func_id = tk.Tk.after(samplerate, self.update_plot) 更改为 self.after(samplerate, self.update_plot)tk.Tk 不是有效值。

标签: python oop tkinter tk


【解决方案1】:

您的问题似乎在您的后声明中。

tk.Tk 不是你想的那样。

在使用after() 时,您经常将其应用于root 窗口,或者在课堂上更频繁地应用于self

所以改变:

tk.Tk.after(samplerate, self.update_plot)

收件人:

self.after(samplerate, self.update_plot)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2018-10-17
    • 2015-09-27
    • 2021-11-03
    • 2020-05-31
    • 2021-11-30
    相关资源
    最近更新 更多