newnewbie
newnewbie

Reputation: 1003

Tkinter - how to disable an existing button

I am trying to disable a button using Tkinter:

from Tkinter import *
import os


class OptionWindow:




    def __init__(self, value):

        self.master = Tk() 
        self.master.minsize(500,500)
        self.b1 = Button(self.master, text = "save Game", command =self.saveGame, state = NORMAL).grid(row = 0, column = 1, sticky = W)

   def saveGame(self):       
        from modules.startingKit import options
        options.saved = True
        self.b1.configure (state = DISABLED)

Yet, for some reason, when I press the "save Game" button, its appearance does not change. What must I do to disable it?

Upvotes: 1

Views: 1300

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385830

You are making a very common mistake, probably because there are several tutorials on the internet which make this same mistake.

In python, if you do x=foo().bar(), x is given the result of bar(). In your code you're doing self.b=Button(...).grid(...). Thus, self.b is set to the result of grid(...). grid(...) always returns None. Because of that, doing self.b.configure(...) is the same as doing None.configure(...) which obviously is not going to do what you think it is going to do.

The solution is to do widget creation and widget layout in separate steps:

self.b1 = Button(...)
self.b1.grid(...)

Upvotes: 4

Related Questions