1

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() 

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

17 Comments

ah wow thank you it works now :) I think i understand why the order of operation is necessary, but can you elaborate on why i need to change it to tk.IntVar() ?
@James IntVar() is a tkinter method. Because you are importing tkinter as tk all tkinter methods required the tk. prefix to call them. This rule applies to all imported libraries. The only time you can do just IntVar() is if you import the method directly with either from tkinter import * to import all content of tkinter or from tkinter import IntVar to import just IntVar directly.
ah ok i understand now, the thing is i used "import tkinter as tk" from another code sample i found on the internet because "from tkinter import *" didn´t work with my code, i didn´t understand why because syntax wise the code shouldn´t change should it?
@James from tkinter import * will work if you remove all the tk. prefixes from your code first. That said it is best to use import tkinter as tk to prevent accidentally overriding built in methods.
would you also recommend "import tkinter as tk" when i want to implement this class into other code i´m working on?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.