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 391,563 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 2,676 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:
Views: 2287 | Replies: 19
Reply
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Question Python GUI Problem

  #1  
Aug 16th, 2007
Ok so I'm trying to use menu bars to grey out different widgets in my gui

il show you all the lines that i think are important and the error messages for each command i have tried.

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

thats assigning the main window as root, and creating a text entry box assigned to input_text with root as its master. The rest of the options there are not important.

These are the commands i tried to get the menu button to grey out the input_text widget.

The first command and error

 input_menu.add_radiobutton(label='Input From File', variable = input_option, command = root.input_text(state = DISABLED)) 


input_menu.add_radiobutton(label='Input From File', variable = input_option, command = root.input_text(state = DISABLED))
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1721, in __getattr__
return getattr(self.tk, attr)
AttributeError: input_text

The next command and error was

 input_menu.add_radiobutton(label='Input From File', variable = input_option, command = input_text(state = DISABLED)) 

input_menu.add_radiobutton(label='Input From File', variable = input_option, command = input_text(state = DISABLED))
TypeError: 'NoneType' object is not callable


Any help with this would be great, I'm sorry if the code isn't all on the right lines.
AddThis Social Bookmark Button
Reply With Quote  
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

  #2  
Aug 16th, 2007
Oh... this is a confusing feature of GUI programming, and especially with Tkinter.

Making root the parent of input_text does *not* automatically make input_text the child of root.

Weird, huh?

Think of it like this: Tkinter is a Python interface to Tk. Thus, there are two systems going on at once -- Tk and Python. The line

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

tells Tk that you want a Text widget that is the child of root.

But Python doesn't know it! To inform Python, you have to do so explicitly:

  1. root.input_text = Text(root, ...) # rest of options go in ...

Now, Python will make input_text a data member of root, which is what you wanted anyways.

Now, the second command just doesn't make sense.

  1. input_menu.add_radiobutton(label='Input From File', variable = input_option, command = [b]input_text(state = DISABLED)[/b])

input_text is a Text widget. The ( ) operator says "call this function." And command requires a function name -- NOT a called function, just the name.

So poor Python is nobly trying to call the Text widget and pass the return value as a function name to the command parameter of input_menu.add_radiobutton().

Not surprisingly, it complains. :lol:

What did you intend here?

Hope it helps,
Jeff
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Python GUI Problem

  #3  
Aug 18th, 2007
Originally Posted by jrcagle View Post

  1. root.input_text = Text(root, ...) # rest of options go in ...

Now, Python will make input_text a data member of root, which is what you wanted anyways.

Would this not mean that your just changing the variable to root.input.text should it not be like this

  1. input_text = root.Text(root, ...) # rest of options go in

Originally Posted by jrcagle View Post
Now, the second command just doesn't make sense.

  1. input_menu.add_radiobutton(label='Input From File', variable = input_option, command = [b]input_text(state = DISABLED)[/b])

What did you intend here?

The command part was intended to mean that when the button was selected it would disable the input_text widget. Should i create a function that does this then put that as the command?

thanks for the help.
Reply With Quote  
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

  #4  
Aug 18th, 2007
Originally Posted by jrcagle
  1. root.input_text = Text(root, ...) # rest of options go in ...
  2.  
  3.  
Now, Python will make input_text a data member of root, which is what you wanted anyways.

Originally Posted by Haze View Post
Would this not mean that your just changing the variable to root.input.text should it not be like this

  1. input_text = root.Text(root, ...) # rest of options go in


Well, here's what the first one means:

* Create a Text widget.
* Assign the return value (the Text widget object itself) to the variable root.input_text.

So yes, it does change the value of root.input_text, by setting it equal to the Text widget. Previously, root.input_text did not exist, so that's not a problem; you haven't clobbered anything.

Here's what the second one means:

* call the function root.Text() (which doesn't exist, since Tk objects don't a function called Text())
* Assign the return value to the local variable input_text.

You don't want that, since input_text will go out of scope when your function ends, and then you lose ability to access it.

I hope that's clear...

Originally Posted by Hazel
The command part was intended to mean that when the button was selected it would disable the input_text widget. Should i create a function that does this then put that as the command?


Ah... yes, you should create a new function and supply the name of that function as the command.

Jeff
Last edited by jrcagle : Aug 18th, 2007 at 2:57 pm.
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Python GUI Problem

  #5  
Aug 19th, 2007
i these are the two variations of the function i use.

def disable(widget):
     widget(state = disabled)

def disable(widget):
     widget(state = DISABLED)

i then changed the command to look like this

root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable(root.input_text))


this command with the first functions gives this error

root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable(root.input_text))
line 4, in disable
widget(state = disabled)
NameError: global name 'disabled' is not defined


the second function gives this error

root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable(root.input_text))
line 4, in disable
widget(state = DISABLED)
TypeError: 'NoneType' object is not callable

i also tried using a simple function with no variables but this gave the same errors as the last 2 depending on if i used caps to write disabled.
Last edited by Haze : Aug 19th, 2007 at 7:35 am.
Reply With Quote  
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

  #6  
Aug 19th, 2007
Right, you want to pass the NAME of the function:

  1. def disable():
  2. root.input_text["state"] = DISABLED
  3.  
  4. root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable)

What you're doing is calling the function *when* you add the radiobutton, then setting command = return value.

Remember that the () operator means "call the function now."

Jeff
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Python GUI Problem

  #7  
Aug 19th, 2007
Originally Posted by jrcagle View Post
  1. def disable():
  2. root.input_text["state"] = DISABLED
  3.  
  4. root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable)


Ok so i have made a function and changed the command
def disable():
     root.input_text(state = DISABLED) #way you wrote it doesn't work

root.input_menu.add_radiobutton(label='Input From File', variable = input_option, command = disable)

the program loads correctly but as soon as i press that button in the GUI i get this error

File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
root.input_text(state = DISABLED)
TypeError: 'NoneType' object is not callable

also even if this way does work it seems like the wrong way to do it because i would have to create a new function for every widget i wish to disable, surely there is a way to pass variables into functions using the command.
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Python GUI Problem

  #8  
Aug 19th, 2007
Posted by jrcagle:
def disable():
root.input_text["state"] = DISABLED

Posted by Haze:
def disable():
root.input_text(state = DISABLED) #way you wrote it doesn't work

Do you see the difference...?

posted by jrcagle:
Remember that the () operator means "call the function now."
you understand this or ...?

Ok, root.input_text is an object NOT function. So in your case you want to set property of this object. So you either call member function or set its object's property in the way,
Posted by jrcagle:
def disable():
root.input_text["state"] = DISABLED

or
root.input_text.config(state = 'DISABLED')



kath.
Last edited by katharnakh : Aug 19th, 2007 at 11:03 pm.
challenge the limits
Reply With Quote  
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

  #9  
Aug 20th, 2007
Here's a working small version:

  1. from Tkinter import *
  2. def disable():
  3. root.button["state"] = DISABLED
  4.  
  5. root = Tk()
  6. root.button = Radiobutton(root, text="My Button")
  7. root.button2 = Button(root, text="Disable the Radiobutton", command=disable)
  8. root.button.grid()
  9. root.button2.grid()
  10. root.mainloop()

Hope it helps,

Jeff
Reply With Quote  
Join Date: Nov 2006
Posts: 23
Reputation: Haze is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Haze Haze is offline Offline
Newbie Poster

Re: Python GUI Problem

  #10  
Aug 29th, 2007
from Tkinter import *

def disable():# temp
    root.input_text['state'] = DISABLED

root = Tk()
root.input_text = Text(root, height = 10, width = 25).grid(row = 1, column = 1, sticky = N+S+E+W)
root.mainbar_menu = Menu(root)
root.input_menu = Menu(root.mainbar_menu)
root.mainbar_menu.add_cascade(label='Input Options', menu = root.input_menu)
root.input_menu.add_radiobutton(label='Input From File', command = disable)
root.config(menu=root.mainbar_menu)

root.mainloop()

This code is taken out of part of the larger program im writing so some of the code may seem pointless, im only keeping it their in case i dont realise how much of a effect it has.

error -
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "C:/Documents and Settings/Owner/Desktop/ex.py", line 4, in disable
root.input_text['state'] = DISABLED
TypeError: 'NoneType' object does not support item assignment

The error occurs when i click the menu button, not when the program loads. What am i doing wrong when im making this menu?

also your example worked fine, although i tried editing it so i would not have to write a new function for every time i want to disable a diffrent widget by changing these lines.

def disable(widget):
    widget["state"] = DISABLED

root.button2 = Button(root, text="Disable the Radiobutton", command=disable(root.button))

this doesnt work as expected because as soon as the gui has loaded the button is allready disabled. sorry i took over a week to get back to you but getting this program to work is not exactly high proiority right now. thanks.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 10:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC