I'm making a simple GUI for the Raspberry Pi using Tkinter. When I try to run it, I get the following error:
self.rpm_status1.config(text=rpm_value)
AttributeError: 'Application' object has no attribute 'rpm_status1'
I'm pretty sure the problem lies in the formatting somewhere but I'm not familiar enough with Python to find the problem. Please also let me know if creating the 'Application' class is best for this type of application, as opposed to some other convention. Here is my code:
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***
You did not make rpmstatus1 accessible by other functions in the class because you did not use self. Here is the corrected code, and should be used for every widget that you plan to use in other functions:
self.rpm_status1 = Label(
root, text="%d RPM", bd=1, relief=SUNKEN)
This ensures that the widget is usable throughout the class.