[py3] Tkinter menu checkbutton not working

D

Dodo

Hello,

I trying to make this piece of code work (this is python3)

from tkinter import *
from tkinter.ttk import *

class Window:
def __init__(self):
self.root = Tk()

self.menu = Menu(self.root)
self.root['menu'] = self.menu

self.submenu = Menu(self.menu)
self.ck = 0
self.submenu.add_checkbutton(label="My checkbutton",
variable=self.ck, command=self.displayCK)
self.menu.add_cascade(label="sub", menu=self.submenu )

def displayCK(self):
print( self.ck )


app = Window()
app.root.mainloop()


The self.ck will always be 0... why?

Dorian
 
R

rantingrick

Hello,

I trying to make this piece of code work (this is python3)

from tkinter import *
from tkinter.ttk import *

class Window:
  def __init__(self):
   self.root = Tk()

   self.menu = Menu(self.root)
   self.root['menu'] = self.menu

   self.submenu = Menu(self.menu)
   self.ck = 0
   self.submenu.add_checkbutton(label="My checkbutton",
variable=self.ck, command=self.displayCK)
   self.menu.add_cascade(label="sub", menu=self.submenu )

  def displayCK(self):
   print( self.ck )

app = Window()
app.root.mainloop()


see my recent post on your last question. The way you are writing
these classes is wrong. Always inherit from something, in this case
Tk. Fix that first and then pretty up this GUI. But to answer your
question "self.ck" needs to be an instance of tk.IntVar. Read more
about it here...

http://infohost.nmt.edu/tcc/help/pubs/tkinter/checkbutton.html
 
T

Terry Reedy

Hello,

I trying to make this piece of code work (this is python3)

from tkinter import *
from tkinter.ttk import *

class Window:
def __init__(self):
self.root = Tk()

self.menu = Menu(self.root)
self.root['menu'] = self.menu

self.submenu = Menu(self.menu)
self.ck = 0
self.submenu.add_checkbutton(label="My checkbutton", variable=self.ck,
command=self.displayCK)
self.menu.add_cascade(label="sub", menu=self.submenu )

def displayCK(self):
print( self.ck )

app = Window()
app.root.mainloop()

The self.ck will always be 0... why?

You never change it ;-)
Passing the *value* 0 to the widget has no effect on the binding of
self.ck. You need to pass a container whose contents the widget can
modify == specifically an IntVar. See 24.1.6.4. Coupling Widget Variables.

Terry Jan Reedy
 
R

rantingrick

<snip>

Also you are now NOT using 8 space indention unlike your last post --
which i applaud you for. However you've gone to the opposite extreme
with 2 space indention and interlaced it with one space indention, oh
dear!

Please use four space indention as this is the preferred and excepted
way to write python code. If you plan to write code that other Python
programmers see you should read the Python style guide and follow it.
Heres a link

http://www.python.org/dev/peps/pep-0008/
 
D

Dodo

Le 09/06/2010 18:54, rantingrick a écrit :
Hello,

I trying to make this piece of code work (this is python3)

from tkinter import *
from tkinter.ttk import *

class Window:
def __init__(self):
self.root = Tk()

self.menu = Menu(self.root)
self.root['menu'] = self.menu

self.submenu = Menu(self.menu)
self.ck = 0
self.submenu.add_checkbutton(label="My checkbutton",
variable=self.ck, command=self.displayCK)
self.menu.add_cascade(label="sub", menu=self.submenu )

def displayCK(self):
print( self.ck )

app = Window()
app.root.mainloop()


see my recent post on your last question. The way you are writing
these classes is wrong. Always inherit from something, in this case
Tk. Fix that first and then pretty up this GUI. But to answer your
question "self.ck" needs to be an instance of tk.IntVar. Read more
about it here...

http://infohost.nmt.edu/tcc/help/pubs/tkinter/checkbutton.html

I already tried with self.ck = IntVar()
and now it displays PY_VAR0

FYI, I'm using Thunderbird 3, which appears to have some bugs with
indentation (according to Alf P. Steinbach). That's why I replaced \t by
a single space
 
R

rantingrick

Le 09/06/2010 18:54, rantingrick a crit :




Hello,
I trying to make this piece of code work (this is python3)
from tkinter import *
from tkinter.ttk import *
class Window:
   def __init__(self):
    self.root = Tk()
    self.menu = Menu(self.root)
    self.root['menu'] = self.menu
    self.submenu = Menu(self.menu)
    self.ck = 0
    self.submenu.add_checkbutton(label="My checkbutton",
variable=self.ck, command=self.displayCK)
    self.menu.add_cascade(label="sub", menu=self.submenu )
   def displayCK(self):
    print( self.ck )
app = Window()
app.root.mainloop()
see my recent post on your last question. The way you are writing
these classes is wrong. Always inherit from something, in this case
Tk. Fix that first and then pretty up this GUI. But to answer your
question "self.ck" needs to be an instance of tk.IntVar. Read more
about it here...

I already tried with self.ck = IntVar()
and now it displays PY_VAR0

FYI, I'm using Thunderbird 3, which appears to have some bugs with
indentation (according to Alf P. Steinbach). That's why I replaced \t by
a single space

IntVar is a class and self.ck is an instance of that class which is a
PY_VAR. Try print(dir(self.ck)) in your callback to see what methods
are available to this instance. Im just speculating here but somehow
there must be a way to "get" and "set" the IntVar's value... hmmm?

You're about to kick yourself when you realize it. ;-)
 
D

Dodo

Le 09/06/2010 20:37, rantingrick a écrit :
Le 09/06/2010 18:54, rantingrick a crit :




I trying to make this piece of code work (this is python3)
from tkinter import *
from tkinter.ttk import *
class Window:
def __init__(self):
self.root = Tk()
self.menu = Menu(self.root)
self.root['menu'] = self.menu
self.submenu = Menu(self.menu)
self.ck = 0
self.submenu.add_checkbutton(label="My checkbutton",
variable=self.ck, command=self.displayCK)
self.menu.add_cascade(label="sub", menu=self.submenu )
def displayCK(self):
print( self.ck )
app = Window()
app.root.mainloop()
see my recent post on your last question. The way you are writing
these classes is wrong. Always inherit from something, in this case
Tk. Fix that first and then pretty up this GUI. But to answer your
question "self.ck" needs to be an instance of tk.IntVar. Read more
about it here...

I already tried with self.ck = IntVar()
and now it displays PY_VAR0

FYI, I'm using Thunderbird 3, which appears to have some bugs with
indentation (according to Alf P. Steinbach). That's why I replaced \t by
a single space

IntVar is a class and self.ck is an instance of that class which is a
PY_VAR. Try print(dir(self.ck)) in your callback to see what methods
are available to this instance. Im just speculating here but somehow
there must be a way to "get" and "set" the IntVar's value... hmmm?

You're about to kick yourself when you realize it. ;-)

thanks I realised it eventually
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top