在root.after()上工作时遇到问题

问题描述:

这里有一些问题的背景:我目前正在尝试在树莓派上显示一个简单的tkinter gui来显示温度计/湿度传感器的传感器读数。然而,在我开始之前,我需要检查是否可以让tkinter用新值更新自己。我已经包含了我正在使用的代码。现在,我试图通过使用root.after以root.after调用change(),它会更新gui中的某个标签,而代码位于root.mainloop()中,以摄氏温度为单位每秒增加1。 )。但是,每次尝试时都会遇到错误,我不确定如何解决此问题。在root.after()上工作时遇到问题

我的代码:

import tkinter as tk 
    from tkinter import ttk 

    class Example(tk.Frame): 

     def __init__(self, parent): 
      tk.Frame.__init__(self, parent, background="white") 

      self.parent = parent 

      self.initUI() 


     def initUI(self): 

      self.parent.title("Interface") 
      self.style = ttk.Style() 
      self.style.theme_use("alt") 

      self.pack(fill=tk.BOTH, expand=1) 

      self.tempLabel = tk.Label(self, text="Temperature", fg="black", bg="white") 
      self.tempLabel.grid(row=0, column=0) 

      self.tempC = tk.Label(self, text="4654645˚ Celsius", fg="black", bg="white") 
      self.tempC.grid(row=1, column=0) 

      self.tempF = tk.Label(self, text="0˚ Fahrenheit", fg="black", bg="white") 
      self.tempF.grid(row=2, column=0) 

      self.rhLabel = tk.Label(self, text="Relative Humidity", fg="black", bg ="white") 
      self.rhLabel.grid(row=0, column=1) 

      self.rh = tk.Label(self, text=" 53.37%", fg="black", bg="white") 
      self.rh.grid(row=1, column=1) 

     def change (self, variable, state): 
      variable + 1 
      newText = str(variable) 

      if state ==1: 
       newText += "%" 
       self.rh.config(text=newText, width =20) 
       self.rh.update_idletasks() 

      elif state ==2: 
       newText += "˚ Celsius" 
       self.tempC.config(text=newText, width =20) 
       self.tempC.update_idletasks() 

      elif state ==3: 
       newText += "˚ Fahrenheit" 
       self.tempF.config(text=newText, width =20) 
       self.tempF.update_idletasks() 

      self.after(1000, self.change) 

def main(): 
    var =0 

    root = tk.Tk() 
    root.geometry("250x250+300+300") 
    app = Example(root) 
    app.change(var, 2) 
    root.mainloop() 


if __name__ == '__main__': 
    main() 

这里是我的错误:(此错误是固定的)

Traceback (most recent call last): 
File "/home/pi/tkinter_test.py", line 108, in <module> 
main() 
File "/home/pi/tkinter_test.py", line 102, in main 
app.change(var, 2) 
File "/home/pi/tkinter_test.py", line 92, in change 
tk.Frame.after(1000, self.change) 
File "/usr/lib/python3.2/tkinter/__init__.py", line 486, in after 
self.tk.call('after', ms) 
AttributeError: 'int' object has no attribute 'tk' 

任何帮助,将不胜感激。我是python的新手,所以代码中的问题可能很愚蠢。

编辑:

每低于凯文的建议下,我改变tk.Frame.after(1000,self.change)到self.after(1000,self.change)。不过,我现在收到此错误:

Exception in Tkinter callback 
Traceback (most recent call last): 
File "/usr/lib/python3.2/tkinter/__init__.py", line 1426, in __call__ 
return self.func(*args) 
File "/usr/lib/python3.2/tkinter/__init__.py", line 490, in callit 
func(*args) 
TypeError: change() takes exactly 3 arguments (1 given) 

它似乎不喜欢我叫self.change不带任何参数,当我在以后使用它()。然而,在网上查找我从来没有看到在after()中给出模块的参数,并且当我添加它们时,我得到递归错误。

+2

我不认为你可以调用'之后'没有提供小部件实例。您可以在'main'中执行'root.after'或在'change'执行'self.after',但不能只执行'tk.Frame.after'。 (我发布这个评论,而不是一个答案,因为我认为在此之前,你会遇到一些其他问题,然后你的代码完美工作,我没有时间来解决它们) – Kevin

+0

是的,使用self.root得到gui出现(但不更新),并出现另一个错误。感谢您的修复。我会尽力去解决这个新错误,但如果我失败了,我会把它添加到上面的帖子中。 – Keenan

+0

阅读错误消息:它告诉你到底发生了什么问题。你设计了'change'以用参数调用,但是当你调用'after'后面的时候你没有传递那些参数。 –

你尝试使用root.update()而不是root.after()吗?对我来说,工作好了很多这样的:

import tkinter as tk 
root = tk.Tk() 
b = Frame(root) 
root.update() 
在这个例子中

我使用root.update代替root.mainloop(),但大公可以合并和root.update()凉爽的部分是,它它确实如此说

+0

'root.update()'在这种情况下没有任何用处。 –

+0

我只是想帮助布赖恩,我不能看到什么是错的 –

+1

是的,我看到在线,你可以把根,更新和root.update_idletasks在一个while循环,它将提供与root.mainloop大致相同的目的。如果我不能使用mainloop工作,我会尝试,但是因为root.mainloop是你应该用tkinter Id来使用它的方式。 – Keenan