1

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?

2
  • Not sure if it matters: Have you started the mainloop? self.master.mainloop() as the last thing in your __init__ function. Or bring the Tk() instance as a parameter to OptionWindow's constructor and pass it to self.master. Commented Mar 28, 2013 at 20:25
  • I have...sorry I did omit that here. Commented Mar 28, 2013 at 20:28

1 Answer 1

4

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(...)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.