tkinter prob

J

JyotiC

hi,
i am making a GUI using Tkinter,
I have a button and a checkbutton.
i want the button to be enable when checkbutton is on and disble when
the checkbutton is off.

thanx
 
F

Fredrik Lundh

JyotiC said:
i am making a GUI using Tkinter,
I have a button and a checkbutton.
i want the button to be enable when checkbutton is on and disble when
the checkbutton is off.

use the checkbutton's command option to install a callback that enables
or disables the button.

</F>
 
J

JyotiC

i have tried it out but it's not working.
this is the code

from Tkinter import *

class abc:
def __init__(self,parent):
#make container myparent
self.myparent=parent
self.myparent.geometry("500x200")

#make the initial frame
self.frame=Frame(self.myparent)
self.frame.pack()

self.var=IntVar()
self.var.set(0)

a=Button(self.frame,text="button")
a.pack()


Checkbutton(self.frame,text="hello",variable=self.var,command=self.com(a)).pack()

def com(self,a):
if self.var.get()==1:
a.config(state=ENABLED)
else:
a.config(state=DISABLED)

root=Tk()
abc(root)
root.mainloop()
 
E

Eric Brunel

i have tried it out but it's not working.
this is the code

from Tkinter import *

class abc:
def __init__(self,parent):
#make container myparent
self.myparent=parent
self.myparent.geometry("500x200")

#make the initial frame
self.frame=Frame(self.myparent)
self.frame.pack()

self.var=IntVar()
self.var.set(0)

a=Button(self.frame,text="button")
a.pack()


Checkbutton(self.frame,text="hello",variable=self.var,command=self.com(a)).pack()

This *calls* self.com(a) and assigns its *results* (which happens to be
None) to the command option in your button. So your button does nothing.

To do what you want, use:

self.a = Button(self.frame,text="button")
self.a.pack()
Checkbutton(self.frame,text="hello",variable=self.var,command=self.com).pack()

in __init__, then:

def com(self):
if self.var.get()==1:
self.a.config(state=ENABLED)
else:
self.a.config(state=DISABLED)

HTH
 
J

JyotiC

thanx
got the error,
but there is one more prob, button is not coming to enable again. once
it is disabled
the error it's giving is:-

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "<stdin>", line 23, in com
NameError: global name 'ENABLED' is not defined
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "<stdin>", line 23, in com
NameError: global name 'ENABLED' is not defined


is ENABLED valid option?
 
P

Peter Otten

JyotiC said:
got the error,
but there is one more prob, button is  not coming to enable again. once
it is disabled
the error it's giving is:-
NameError: global name 'ENABLED' is not defined

Try NORMAL instead of ENABLED.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top