unexplained behavior of tkMessageBox.askyesno

P

Peter Kleiweg

I have a program with these fragments:



from Tkinter import *
import tkFileDialog
import tkMessageBox

def openProject():
filepath = tkFileDialog.askopenfilename(filetypes=(("project files","*.ini"), ("all","*")))
if filepath:
loadProject(filepath)

def makeClean():
if tkMessageBox.askyesno('Make clean', 'Remove all files created by Make?'):
print 'yes'
# more code



If I call makeClean(), I get the yes/no box. If I click 'yes',
it prints 'yes', unless I have called openProject() sometimes
earlier. In the latter case, none of the code following the
askyesno gets executed, whether I click 'yes' or 'no'. It also
happens when I have called openProject() earlier and clicked
'Cancel'.

This error happens on one Linux machine ([GCC 3.3.1 (SuSE
Linux)] on linux2), but not on another ([GCC 2.95.3 20010315
(SuSE)] on linux2). Both have Python 2.3.4. It works fine on
Windows98 with Python 2.4

Anyone any idea what is going on?
 
P

Peter Kleiweg

Peter Kleiweg schreef:
from Tkinter import *
import tkFileDialog
import tkMessageBox

def openProject():
filepath = tkFileDialog.askopenfilename(filetypes=(("project files","*.ini"), ("all","*")))
if filepath:
loadProject(filepath)

def makeClean():
if tkMessageBox.askyesno('Make clean', 'Remove all files created by Make?'):
print 'yes'
# more code


I changed the code to this:

def makeClean():
print tkMessageBox.askyesno('Make clean', 'Remove all files created by Make?')


If I have used openProject(), the function makeClean() always
prints 'False', no matter what I choose.
 
J

Jeremy Bowers

If I have used openProject(), the function makeClean() always
prints 'False', no matter what I choose.

If I had to guess, I'd say the problem is that you aren't using Tk
correctly. You really need to run

root = Tk()
root.mainloop()

to be confident everything will work as advertised. Which means you'll
need to re-structure your program a bit.

Tk, as GUI toolkits go, is surprisingly effective without its mainloop
running; many GUI toolkits won't even get that far. However, in my
experience, you'll get random-seeming failures from anything that requires
events to work properly, and "mouse clicking" is an event.
 
P

Peter Kleiweg

Jeremy Bowers schreef:
If I had to guess, I'd say the problem is that you aren't using Tk
correctly. You really need to run

root = Tk()
root.mainloop()


I have that.

The tkMessageBox.askyesno() is called from a function that is a
callback from a menu item attached to root.
 
P

Peter Kleiweg

Peter Kleiweg schreef:
I have a program with these fragments:



from Tkinter import *
import tkFileDialog
import tkMessageBox

def openProject():
filepath = tkFileDialog.askopenfilename(filetypes=(("project files","*.ini"), ("all","*")))
if filepath:
loadProject(filepath)

def makeClean():
if tkMessageBox.askyesno('Make clean', 'Remove all files created by Make?'):
print 'yes'
# more code

The file tkMessageBox.py that comes with Python has this:

def askyesno(title=None, message=None, **options):
"Ask a question; return true if the answer is yes"
s = _show(title, message, QUESTION, YESNO, **options)
return s == YES


So instead of calling askyesno, I used this:

def makeClean():
r = tkMessageBox._show('Make clean', 'Remove all files created by Make?', 'question', 'yesno')
print type(r), r

Call makeClean(), click no, result: <type 'str'> no
Call makeClean(), click yes, result: <type 'str'> yes
Call openProject(), click cancel
Call makeClean(), click no, result: <type 'str'> no
Call makeClean(), click yes, result: <type 'bool'> True

Now, how did that happen?
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top