User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 423,618 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,165 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums

Python GUI Problem

Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Python GUI Problem

  #12  
Sep 5th, 2007
Sorry Haze; I mistook "not exactly high priority" to mean "not worth investing in."

This line

  1. root.input_text = Text(root, height = 10, width = 25).grid(row = 1, column = 1, sticky = N+S+E+W)

does the following:

* creates a new Text widget
* calls its .grid() method, which returns None
* then assigns it to root.input_text

So no matter what, root.input_text == None, NOT a Text widget.

Then later, when you call disable(), Surprise! 'None' doesn't have any methods.

BTW, in debugging things like this, I *always* print values of variables just to make sure that they are what I think they are...

What you really want is this:

  1. root.input_text = Text(root, height = 10, width = 25)
  2. root.input_text.grid(row = 1, column = 1, sticky = N+S+E+W)

Now with regard to the second problem,

  1. def disable(widget):
  2. widget["state"] = DISABLED
  3.  
  4. root.button2 = Button(root, text="Disable the Radiobutton", command=disable(root.button))

You can probably predict why the root.button is disabled from the beginning -- when you create root.button2, you *call* disable(root.button)!

So in your code, the act of creating root.button2 causes root.button to be disabled. Definitely not what you wanted.

I would solve your problem by using classes. What you want, if I read correctly, is for a bunch of buttons to have their own special "targets" that they can disable. Yes?

So what you really want is a type of Button() with a disable function attached to it. That calls for a new class that inherits from Button:

  1. from Tkinter import *
  2.  
  3. class DisablerButton(Button):
  4.  
  5. def __init__(self, master, target, cnf={}, **kw):
  6. Button.__init__(self, master, cnf, **kw)
  7. self.target = target
  8. self['command'] = self.disable
  9.  
  10. def disable(self):
  11. self.target["state"] = DISABLED
  12.  
  13. mainw = Tk()
  14. mainw.b1 = Button(mainw, height=5,width=10, text="My Button")
  15. mainw.b2 = DisablerButton(mainw, mainw.b1, height=5, width=10, text="Press Me!")
  16. mainw.b3 = DisablerButton(mainw, mainw.b2, height=5,width=10, text="Me Too!")
  17. mainw.b1.grid()
  18. mainw.b2.grid()
  19. mainw.b3.grid()
  20. mainw.mainloop()

There's a couple things that need explaining. First, the __init__ method for Button can take a lot of arguments. The **kw refers to all of the configuration arguments like width= and height=. cnf is a less commonly used way to package those configuration options into a dictionary. Rather than spell all those arguments out, I just package them up and pass them upstairs to Button.__init__.

Second, notice that this way of doing things allows you to specify a different target for each individual DisablerButton widget. I think this is what you wanted.

Can you see why a class is required to do this?

Jeff
Reply With Quote  
All times are GMT -4. The time now is 9:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC