1

I tried to create a simple GUI with one check button and one normal button.

The goal of the program is simple:

  • if you check or uncheck the check button the normal button should go into disabled mode

What went wrong?

  • at first I started to get errors like “AttributeError: 'NoneType' object has no attribute 'config'” I said to myself what “NoneType” object?

  • Then I discovered that my Button object right after its creation disappears somewhere (I create it on line 16 and on line 17 I get None when I try to retrieve it).

  • is it normal behavior or am I doing something terribly wrong? Or is there another way how to retrieve the button instance and work with it?

The weird thing is that the object still displays on the screen: enter image description here

My main script:

from tkinter import *
from import_test import GUI

root = Tk() #create root
my_gui = GUI(root) #pass it to my GUI

root.mainloop() #loop

And here is the sub script with the GUI class (I have created init.py file and everything that I needed):

from tkinter import *
from tkinter import ttk

class GUI:
    def __init__(self, root):
        self.root = root
        self.tabControl = ttk.Notebook(root)

        #Tabs and tab control
        self.tab = Frame(self.tabControl)
        self.tabControl.add(self.tab, text = "Something")

        #Buttons + check button
        self.checkVal = IntVar()
        self.checkButton = Checkbutton(self.tab, text = "Control", variable = self.checkVal, command = self.checkFunction).grid(row = 0, column = 0) 
        self.button = Button(self.tab, text = "START").grid(row = 1, column = 0) 
        print("This should be my button: ", self.button) #always results in "None"

        self.tabControl.pack(expan = 1, fill = "both")

    #if button is checked - disable the button
    def checkFunction(self):
        print(self.button) #always results in "None"
        self.button.config(state=DISABLED) #throws AttributeError: 'NoneType' object has no attribute 'config'
Jakub Szlaur
  • 1,852
  • 10
  • 39

1 Answers1

3

You called .grid on the Button before assigning it; like most mutating methods on mutable objects in Python, grid modifies the object in question and returns None. Assign the value, then grid it, and you won't have the problem:

    self.checkButton = Checkbutton(self.tab, text = "Control", variable = self.checkVal, command = self.checkFunction)
    self.checkButton.grid(row = 0, column = 0) 
    self.button = Button(self.tab, text = "START")
    self.button.grid(row = 1, column = 0) 
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271