而在蟒蛇編碼我有錯誤。 需要再次重新&打印相同的隨機值。Python的錯誤; 「NoneType」對象有「後」沒有屬性
from Tkinter import *
import Tkinter
from random import randint
class App1(Tkinter.Tk):
def __init__(self, master):
Tkinter.Tk.__init__(self,master)
self.master = master
self.label = Label(self , text = "Voltage" , font = ("Helvetica",32))
self.label.grid(row = 0)
self.reading_label = Label(self,text = '0.0' , font = ("Helvetica",110))
self.reading_label.grid(row = 1)
self.update_reading()
def update_reading(self):
value = randint(0,9)
reading_str = "{:.2f}".format(value)
self.reading_label.configure(text = reading_str)
self.master.after(1 , self.update_reading)
root = App1(None)
root.title("Option")
root.geometry("320x240")
root.mainloop()
我收到錯誤消息:
Traceback (most recent call last):
File "/voltmeter.py", line 26, in <module>
root = App1(None)
File "/voltmeter.py", line 16, in __init__
self.update_reading()
File "/voltmeter.py", line 23, in update_reading
self.master.after(1 , self.update_reading)
AttributeError: 'NoneType' object has no attribute 'after'
看起來你永遠不會初始化'master'的任何東西,因爲你是路過'None'到'__init__'方法。 「主」應該是什麼類型? – Engineero