pygtk 弹出窗口无序执行
我有 pygtk 菜单,其中在 menuitem 单击时调用一个函数,在这个函数中,我向用户显示一个弹出窗口,提示用户在检查互联网连接时等待,然后调用一个检查互联网连接的函数,
我的问题是我的程序首先调用互联网连接检查功能,然后在函数调用完成后,它会向我显示弹出窗口,
我尝试放置,
while gtk.events_pending():
gtk.main_iteration_do(False)
它显示模糊弹出窗口,该弹出窗口会一直挂起,直到我的函数调用完成,然后变得清晰。
我的代码看起来像,
dialog = gtk.MessageDialog(
parent = None,
flags = gtk.DIALOG_DESTROY_WITH_PARENT,
type = gtk.MESSAGE_INFO,
buttons = gtk.BUTTONS_NONE,
message_format = None)
dialog.set_markup("Please wait while checking internet connectivity")
dialog.set_title('Checking internet')
dialog.set_position(gtk.WIN_POS_CENTER)
dialog.connect('response', self.show_hide, dialog )
dialog.show()
gobject.timeout_add(5, self.show_hide, dialog)
try :
netStatus = check_network()
except Exception, excp:
print excp
有人可以告诉我出了什么问题吗? 提前致谢...
I have pygtk menu, in which a function is called on menuitem click, in this function i am showing a popup to user saying wait while checking internet connectivity and then calling a function which checks internet connectivity,
My problem is the my programs is first calling the internet connectivity check function and then after completion of function call it shows me popup,
I tried putting,
while gtk.events_pending():
gtk.main_iteration_do(False)
It shows blur popup which hangs till my function call is completed and then gets clear.
my code looks something like,
dialog = gtk.MessageDialog(
parent = None,
flags = gtk.DIALOG_DESTROY_WITH_PARENT,
type = gtk.MESSAGE_INFO,
buttons = gtk.BUTTONS_NONE,
message_format = None)
dialog.set_markup("Please wait while checking internet connectivity")
dialog.set_title('Checking internet')
dialog.set_position(gtk.WIN_POS_CENTER)
dialog.connect('response', self.show_hide, dialog )
dialog.show()
gobject.timeout_add(5, self.show_hide, dialog)
try :
netStatus = check_network()
except Exception, excp:
print excp
Can somebody tell me whats wrong??
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主循环调用菜单项的处理程序,并等待它返回,然后再执行其他操作,包括关闭菜单以便对话框可以获得焦点。
最简单的解决方法可能是在主循环完成时(即显示对话框后)使用 timeout_add 调用连接检查。
The main loop calls your handler for the menu item and waits for it to return before it does anything else, including dismissing the menu so your dialog can get the focus.
The easiest workaround might be to use timeout_add to call your connection check when the main loop gets around to it, i.e. after showing the dialog.
您需要重构 check_network() 以使其非阻塞。
不要在某个套接字上进行阻塞调用,而是使用 gobject.io_add_watch() 注册一个回调,一旦套接字准备好数据,就会收到通知。在此回调中,您可以更新或销毁弹出窗口。
在 Linux 上,GTK 很可能会在后台使用 select 系统调用,该调用可以等待任意数量的带有超时的文件描述符事件,包括套接字和 FIFO(如果您正在生成子进程)。
这个概念称为“基于事件的编程”。您只有一个执行线程。几乎每个 GUI 框架都是这样工作的。这个想法是,你永远不会做任何可能需要超过几毫秒的事情。这是一个很棒的概念,因为它避免了同步真正并发线程时出现的所有棘手问题。
You need to refactor check_network() to make it non-blocking.
Instead of making a blocking call on some socket, use gobject.io_add_watch() to register a callback that will be be notified as soon as data is ready for the socket. In this callback, you can update or destroy the popup.
On Linux GTK will most likely use the select system call behind the scene, which can wait on any number of file descriptor events with a timeout, including sockets and FIFOs (in case you are spawning a subprocess).
This concept is called "event-based programming". You have only a single thread of execution. Almost every GUI framework works this way. The idea is that you never do anything that might take longer than a few milliseconds. It's a great concept, because it avoids all the hairy problems that arise with synchronization of truly concurrent threads.