views:

268

answers:

2

Noob @ programming with python and pygtk.

I'm creating an application which includes a couple of dialogs for user interaction.

#!usr/bin/env python
import gtk
info = gtk.MessageDialog(type=gtk.DIALOG_INFO, buttons=gtk.BUTTONS_OK)
info.set_property('title', 'Test info message')
info.set_property('text', 'Message to be displayed in the messagebox goes here')
if info.run() == gtk.RESPONSE_OK:
    info.destroy()

This displays my message dialog, however, when you click on the 'OK' button presented in the dialog, nothing happens, the box just freezes. What am I doing wrong here?

A: 

can you give me a last chance? ;)

there are some errors in your code:

  • you did not close a bracket

  • your syntax in .set_property is wrong: use: .set_property('property', 'value')

but i think they are copy/paste errors.

try this code, it works for me. maybe did you forget the gtk.main()?

import gtk

info = gtk.MessageDialog(buttons=gtk.BUTTONS_OK)
info.set_property('title', 'Test info message')
info.set_property('text', 'Message to be displayed in the messagebox goes here')
response = info.run()
if response == gtk.RESPONSE_OK:
    print 'ok'
else:
    print response
info.destroy()

gtk.main()
mg
OK. You're right, I do have bad syntax there, but that's just copy/paste errors, I do have the correct syntax in my code.I just tried your suggestion, and it does print 'ok' indicating that the test results are correct on the response, however, the message dialog is still solidly frozen on the screen after I press the OK button.I must be missing something else.
M0E-lnx
A: 

@mg My bad. Your code is correct (and I guess my initial code was too) The reason my dialog was remaining on the screen is because my gtk.main loop is running on a separate thread.

So all I had to was enclose your code (corrected version of mine) in between a

gtk.gdk.threads_enter()

and a

gtk.gdk.threads_leave()

and there it was. Thanks for your response.

M0E-lnx