Hello everyone,

I have a problem with TkMessageBox on Windows:
My program receives a message from a server and then it should show a MessageBox, but instead of doing so, the program freezes.

Elsewhere in the program, a MessageBox is called after the user having pressed a button, and this one is shown.

This problem only occurs on Windows; I've tested it on Mac and there everything works fine.

Can anyone help me?
Thanks in advance

Fridericus

Without some pertinent code it will be impossible to help you. Which program/server event triggers the message box to appear?

Here are some extracts of the code. Hope it helps.

# the receiving of the data
data = s.recv(10000)
print data
    if data[:7] == 'invite ':
        semicolon = data.index(';')
        menus.__invitation__(
            data[7:semicolon],
            data[semicolon+1:-1])

# now the method __invitation__ of menus:

def __invitation__(self, address, name):
    print 'Invitation received'
    text = ('Player "' + name +
            '" on computer ' + address + ' hat sent an invitation \n\nDo you accept it?')
    print text
    if tkMessageBox.askyesno(title='Invitation', message =text) == 1:
        print 'Invitation accepted.'
        # ...

The problem has nothing to do with the server/network stuff. The method is called and 'Invitation received' and the text of the MessageBox are also printed, but then the MessageBox is not shown.

Since I have another MessageBox in the program which is called when a button is pressed and this MessageBox is shown, it seems to have something to do with the circumstance that it is no user action which triggers the appearance of the MessageBox.

As I said, the problem only occurs on Windows. (XP, Python 2.6)

Edited 6 Years Ago by Fridericus: n/a

On my machine, Slackware Linux, the message box returns' True' not 1, which may be causing part of the problem. Try the code below and see what it is on your OS. Also, you can just use
if tkMessageBox.askyesno(title='Invitation', message =text):
which would take care of both situations.

import tkMessageBox

def __invitation__( address, name):
    print 'Invitation received'
    text = ('Player "' + name +
            '" on computer ' + address + ' hat sent an invitation \n\nDo you accept it?')
    print text
    result = tkMessageBox.askyesno(title='Invitation', message =text) 
    print("result =", result)
    if result == 1:
        print 'Invitation accepted.'
        
__invitation__('123 Main St.', 'Harry')

No, that is not the reason.

For testing purpose I've just created a button in my program which invokes the invitation method, and when I click this button, the MessageBox is shown. Only when it's not a user action which invokes the method, the MessageBox is not shown.

I don't do Windows either, but you could simply use a variable as a bool. Try:

result = tkMessageBox.askyesno(title='Invitation', message =text) 
if result:
    print 'Invitation accepted.'

I just ran into a problem with a Tkinter gui I've been working on my laptop. The above format worked on my laptop, but on the house desktop, it somehow got itself reversed. I had to change it to:

result = tkMessageBox.askyesno(title='Invitation', message =text)
    if not result:
print 'Invitation accepted'

Try both ways. I don't know why all of a sudden the 'yes' box returns False, but it's that way on my home comp but returns True on my laptop.

I just realized that your problem is the messagebox not showing up. My bad. Is there something else running that hasn't finished yet? I ran into a few problems where my messageboxes never came up because they were waiting for another process to finish. I fixed it by using the thread module and putting my messagebox in a function. Then call the function with thread.start_new(mbox_func, ()).

Hello.
Sorry for the late response, but I didn't have access to a Windows computer. I've tried your idea, but unfortunately it didn't work. Now I've created a simple client/server example with the same error as in my project, so you can test it on your computers.
Run the server and then the client on a command line. When you enter text at the client, it is printed on the window of the server (to demonstrate that the server receives). When you enter 'invite', the server tries to create a MessageBox, and, at least on my computer, this doesn't work.
Perhaps this helps you in finding the problem. Thank you again for your help.

Attachments
import socket
import sys

host = raw_input('Hostname: ')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 6789))
while 1:
    z = raw_input()
    s.send(z)
    if not z: 
        s.close()
	break
    else:
	data = s.recv(1024)
        print 'Received', repr(data)
import socket
import sys
import threading
import tkMessageBox
from Tkinter import *


class Receive(threading.Thread):
        
    def run(self):
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind(('', 6789))
	s.listen(1)
	conn, addr = s.accept()	
	print 'Connected with Client.'
	while 1:
            data = conn.recv(10000)
	    conn.send(data)
	    if data:
	        try: text.delete(1.0, END)
 	        except: pass
	        text.insert(END, data)
                if data == 'invite':
                    tkMessageBox.showinfo('test', 'test')
	    else:
		conn.close()
		root.destroy()
		break

global root
root = Tk()
frame = Frame(root)
frame.pack()
text = Text(frame)
text.pack()

Receive().start()
root.mainloop()
This article has been dead for over six months. Start a new discussion instead.