我正在使用Tkinter为Raspberry Pi制作一个简单的GUI。当我尝试运行它时,我收到以下错误:
self.rpm_status1.config(text=rpm_value)
AttributeError: 'Application' object has no attribute 'rpm_status1'
我很确定问题在于某处的格式化,但我对Python不熟悉以找到问题。如果创建“应用程序”类最适合此类应用程序,请告诉我,而不是其他约定。这是我的代码:
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import tkinter.messagebox
except ImportError:
import tkMessageBox
import smbus
bus = smbus.SMBus(1)
addr = 0x45
rpm_value = 123
cmd_null = 0
cmd_pwm_on_off = 1
cmd_pwm_select = 2
cmd_pwm_dc = 3
cmd_pwm_period = 4
cmd_rpm_on_off = 5
cmd_rpm_data_prep = 6
cmd_measure = 79
cmd_measure_data_prep = 8
cmd_buzzer_on_off = 9
cmd_batt_data_prep = 10
class Application:
def __init__(self, master):
self.master = master #IDK what this does
#***PWM***
pwm_chkbtn = Checkbutton(root, text="PWM on")
freq_label = Label(root, text="Frequency (Hz):")
freq_entry = Entry(root)
dc_label = Label(root, text="Duty Cycle (%):")
dc_scale = Scale(root, from_=0, to=100, resolution=5, orient=HORIZONTAL)
pwm_chkbtn.grid(columnspan=2)
freq_label.grid(row=1, sticky=E)
freq_entry.grid(row=1, column=1)
dc_label.grid(row=2, sticky=E)
dc_scale.grid(row=2, column=1)
#***RPM***
self.rpm_onoff = IntVar()
rpm_chkbtn = Checkbutton(
root, text="Take RPM", variable=self.rpm_onoff)
rpm_chkbtn.grid(row=3)
rpm_status1 = Label(
root, text="%d RPM", bd=1, relief=SUNKEN)
rpm_status2 = Label(
root, text="%d Hz", bd=1, relief=SUNKEN)
rpm_status1.grid(row=3,column=1, sticky=W, padx=4)
rpm_status2.grid(row=3,column=1)
self.rpm_poll() #start polling
#***resistance***
def take_meas():
bus.write_byte(addr, cmd_measure)
meas_btn = Button(root, text="Take resistance\nmeasurement", command=take_meas)
meas_label = Label(root, text="%d mOhm", bd=1, relief=SUNKEN)
meas_btn.grid(row=4)
meas_label.grid(row=4, column=1, sticky=W, padx=4)
#RPM functions
def rpm_poll (self):
if self.rpm_onoff:
global rpm_value
self.rpm_status1.config(text=rpm_value)
self.master.after(1000, self.poll)
#**main loop**
root = Tk()
root.title("EMC Lab")
app = Application(root)
root.mainloop()
#***end main***
答案 0 :(得分:0)
您没有让类中的其他函数访问rpmstatus1,因为您没有使用self。以下是更正后的代码,应该用于您计划在其他功能中使用的每个小部件:
self.rpm_status1 = Label(
root, text="%d RPM", bd=1, relief=SUNKEN)
这确保了小部件在整个班级中都可用。