Reputation: 327
Hey guys i´m new to tkinter and i´m testing stuff out. In this code i´m getting the error "'Graphicaluserinterface' object has no attribute 'var'" which i don´t understand, since i have it in my init method. My Code:
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.create_widgets()
self.startbuttonfunktion()
self.checkbutton1
self.var=IntVar()
def create_widgets(self):
self.programmstart = tk.Button(self, text = "Programmstart")
self.programmstart.grid(row=0,column=1)
self.programmstart["command"]=self.startbuttonfunktion
self.programmschliessen = tk.Button(self, text ="Exit Programm",command=root.destroy)
self.programmschliessen.grid(row=1,column=2)
self.checkbutton1 = tk.Checkbutton(self, text = "Sensoren1",variable=self.var,onvalue=1,offvalue=0)
self.checkbutton1.grid(row=1,column=0)
def startbuttonfunktion(self):
if self.var.get()==1:
print("Der Checkbutton wurde geklickt")
else:
print("Der Checkbutton wurde NICHT geklickt")
root = tk.Tk()
app = Graphicaluserinterface(master=root)
app.master.title("TestProgramm")
app.master.maxsize(1200,600)
app.mainloop()
Upvotes: 1
Views: 348
Reputation: 15226
The problem is order of operation. You are only creating the self.var
IntVar after you run your other methods. So when those methods try to access self.var
they cannot because it has not been created yet.
Move self.var=IntVar()
Above self.create_widgets()
.
That said you need to also change IntVar()
to tk.IntVar()
as you have shown you are doing import tkinter as tk
and thus all tkinter widgets will need a tk.
prefix.
Make those 2 changes and you code will work fine.
Upvotes: 2