GTK+3 + Python: "Loading..." 对话框
GTK+3 + Python: "Loading..." dialog
我的主应用程序中有一个函数如下所示:
def foo(stuff):
a_line_that_takes_a_while(stuff)
return result
我正在尝试添加一个对话框以在 a_line_that_takes_a_while 之前显示并在该行执行后立即销毁它。
我试过:
def foo(stuff):
dialog = Gtk.MessageDialog(...)
dialog.show_all()
a_line_that_takes_a_while(stuff)
dialog.destroy()
return result
但令人惊讶的是,对话框恰好在 a_line_that_takes_a_while 已经执行时出现。当然我不能使用 dialog.run() 因为那会阻塞我的应用程序的主循环。
有什么想法吗?
您对 GTK 的调用实际上所做的就是对主循环期间发生的操作进行排队。对话框在某种程度上是一种特殊情况,当您调用 dialog.run() 以阻止允许某些更新时。您的 foo 函数指示 GTK 创建一个对话框然后销毁它,甚至在它开始尝试完成工作之前。
Threads should do the job. The biggest gotcha here is that GTK is NOT thread safe. Therefore be careful if you decide to use native python threading. Also, if you are doing disk operations consider GFile 的异步回调。他们可能会为您节省一些重新发明轮子的时间。
我的主应用程序中有一个函数如下所示:
def foo(stuff):
a_line_that_takes_a_while(stuff)
return result
我正在尝试添加一个对话框以在 a_line_that_takes_a_while 之前显示并在该行执行后立即销毁它。
我试过:
def foo(stuff):
dialog = Gtk.MessageDialog(...)
dialog.show_all()
a_line_that_takes_a_while(stuff)
dialog.destroy()
return result
但令人惊讶的是,对话框恰好在 a_line_that_takes_a_while 已经执行时出现。当然我不能使用 dialog.run() 因为那会阻塞我的应用程序的主循环。
有什么想法吗?
您对 GTK 的调用实际上所做的就是对主循环期间发生的操作进行排队。对话框在某种程度上是一种特殊情况,当您调用 dialog.run() 以阻止允许某些更新时。您的 foo 函数指示 GTK 创建一个对话框然后销毁它,甚至在它开始尝试完成工作之前。
Threads should do the job. The biggest gotcha here is that GTK is NOT thread safe. Therefore be careful if you decide to use native python threading. Also, if you are doing disk operations consider GFile 的异步回调。他们可能会为您节省一些重新发明轮子的时间。