By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Tkinter check box behaviour - Windows / Linux discrepancy

P: n/a
I've come across a weird difference between the behaviour of the
Tkinter checkbox in Windows and Linux. The issue became apparent in
some code I wrote to display an image in a fixed size canvas widget. If
a checkbox was set then the image should be shrunk as necessary to fit
the canvas while if cleared it should appear full size with scrollbars
if necessary.

The code worked fine under Linux (where it was developed). But under
Windows, the first click in the checkbox did nothing, then subsequent
clicks adjusted the size according to the PREVIOUS, not the current,
checkbox state.

I've isolated the problem in the code below, which shows a single
checkbox and a label to describe its state. It works ok under Linux,
but in Windows it is always one click behind.

Any ideas? I am using
Linux: Fedora Core 3, Python 2.3.4
Windows: Windows NT, Python 2.3.4

Peter

================================================== ======================
import Tkinter as tk

class App:

def __init__(self,frmMain):

"""
Demonstrate difference in Windows / Linux handling of check box

Text in lblTest should track check box state
"""

# Set up form

self.intTest=tk.IntVar()

self.chkTest=tk.Checkbutton(frmMain,text='Click
me!',variable=self.intTest)

self.chkTest.grid(row=0,column=0,padx=5,pady=5,sti cky=tk.W)

self.chkTest.bind('<ButtonRelease-1>',self.chkTest_click)

self.lblTest=tk.Label(frmMain,text='Dummy')

self.lblTest.grid(row=1,column=0,padx=5,pady=5,sti cky=tk.W)
self.chkTest_click() # so as to initialise text

def chkTest_click(self,event=None):
# read check box state and display appropriate text
if self.intTest.get()==0:
self.lblTest.config(text='Check box cleared')
else:
self.lblTest.config(text='Check box set')


if __name__=='__main__':

frmMain=tk.Tk()

app=App(frmMain)

frmMain.mainloop()

Nov 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
At Thursday 9/11/2006 20:28, peter wrote:
>I've come across a weird difference between the behaviour of the
Tkinter checkbox in Windows and Linux. The issue became apparent in
some code I wrote to display an image in a fixed size canvas widget. If
a checkbox was set then the image should be shrunk as necessary to fit
the canvas while if cleared it should appear full size with scrollbars
if necessary.

The code worked fine under Linux (where it was developed). But under
Windows, the first click in the checkbox did nothing, then subsequent
clicks adjusted the size according to the PREVIOUS, not the current,
checkbox state.

I've isolated the problem in the code below, which shows a single
checkbox and a label to describe its state. It works ok under Linux,
but in Windows it is always one click behind.
self.chkTest=tk.Checkbutton(frmMain,text='Click
me!',variable=self.intTest)

self.chkTest.bind('<ButtonRelease-1>',self.chkTest_click)
Your code works fine in Linux just by accident. The action of button
widgets must be associated to "command"; as you see, when the mouse
button is released, the associated variable might not have been updated.
Remove that bind call, and use:

self.chkTest=tk.Checkbutton(frmMain,text='Click
me!',variable=self.intTest, command=chkTest_click)

(Should work fine on every platform - I've just tested on Windows)
The command is fired after the mouse up event, and *only* if the
previous mouse down was over the same widget. Your code doesn't work
either if the user presses SPACE to toggle the button state; using
"command" works fine in this case too.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 10 '06 #2

P: n/a
Peter,
You already have an answer to you question but if
you want to fancy up your program you could
replace;

self.chkTest.bind('<ButtonRelease-1>',
self.chkTest_click)
>
with
>
self.chkTest.bind('<f2>',self.chkTest_click0)

or some other acceptable key from the keyboard
>

def chkTest_click0(self,event):
self.chkTest_click()
def chkTest_click(self):
# read check box state and display
appropriate text
if self.intTest.get()==0:
self.lblTest.config(text='Check box
cleared')
else:
self.lblTest.config(text='Check box
set')

jim-on-linux
http://www.inqvista.com

On Thursday 09 November 2006 18:28, peter wrote:
I've come across a weird difference between the
behaviour of the Tkinter checkbox in Windows
and Linux. The issue became apparent in some
code I wrote to display an image in a fixed
size canvas widget. If a checkbox was set then
the image should be shrunk as necessary to fit
the canvas while if cleared it should appear
full size with scrollbars if necessary.

The code worked fine under Linux (where it was
developed). But under Windows, the first click
in the checkbox did nothing, then subsequent
clicks adjusted the size according to the
PREVIOUS, not the current, checkbox state.

I've isolated the problem in the code below,
which shows a single checkbox and a label to
describe its state. It works ok under Linux,
but in Windows it is always one click behind.

Any ideas? I am using
Linux: Fedora Core 3, Python 2.3.4
Windows: Windows NT, Python 2.3.4

Peter

===============================================
========================= import Tkinter as tk

class App:

def __init__(self,frmMain):

"""
Demonstrate difference in Windows /
Linux handling of check box

Text in lblTest should track check box
state """

# Set up form

self.intTest=tk.IntVar()
self.chkTest=tk.Checkbutton(frmMain,text='Click
me!',variable=self.intTest)
self.chkTest.grid(row=0,column=0,padx=5,pady=5,
sticky=tk.W)
self.chkTest.bind('<ButtonRelease-1>',self.chkT
est_click)


self.lblTest=tk.Label(frmMain,text='Dummy')
self.lblTest.grid(row=1,column=0,padx=5,pady=5,
sticky=tk.W)
self.chkTest_click() # so as to
initialise text

def chkTest_click(self,event=None):
# read check box state and display
appropriate text if self.intTest.get()==0:
self.lblTest.config(text='Check box
cleared') else:
self.lblTest.config(text='Check box
set')


if __name__=='__main__':

frmMain=tk.Tk()

app=App(frmMain)

frmMain.mainloop()
Nov 10 '06 #3

P: n/a
Thank you for those suggestions

I've tried it on Windows and it seems fine (with the minor change to
command=self.chkTest_click). I'm currently at work, with no access to
Linux, so can't test it there until this evening.

Muchas gracias!

Nov 10 '06 #4

P: n/a
Thank you for those suggestions

I've tried it on Windows and it seems fine (with the minor change to
command=self.chkTest_click). I'm currently at work, with no access to
Linux, so can't test it there until this evening.

Muchas gracias!

Nov 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.