使用GTK窗口等待响应 [英] Using a GTK window and waiting for the response

查看:185
本文介绍了使用GTK窗口等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个应用程序中使用PyGTK来获取用户输入。我正在建立一个有很多检查按钮的窗口,并根据回应我需要继续。



问题是在python当我调用窗口的功能及其操作时,程序不等待窗口的响应?如何使其能够等待窗口的响应?



我也不知道我是否可以使用对话框而不是窗口,因为我尝试添加检查按钮对话框,并没有很好的解决。如果使用窗口不适合任何人可以帮助我对话?



感谢您的帮助。

解决方案

(古老的问题,但它值得回答)



虽然pmoleri给出了可以接受的答案(这是我最初处理这种情况)我发现添加回调是令人沮丧的,因为它会混淆程序流。有一个类似于gtk.Dialog的窗口很整洁,您可以在其中调用window.run(),并将阻止它,直到您得到响应,但GUI仍然可以运行。您可以使用gtk.main()来执行此功能:

  import gtk 
class BlockingWindow(gtk.Window) :
def __init __(self,checkmark_values):
gtk.Window .__ init __(self)
self.checkmarks = {}
#在这里创建复选标记,把它们放在一个字典
#将他们的名字与他们的小部件相关联
#另外创建ok按钮
ok_button.connect(clicked,self.on_ok_button_clicked)

def on_ok_button_clicked(self,button ):
gtk.main_quit()

def run(self):
self.show_all()
gtk.main()
self.destroy ()
在self.checkmarks中为x返回dict((x,self.checkmarks [x] .get_active())

blocking_window = BlockingWindow([option1,option2 ]
values = blocking_window.run()
printoption1 is {}format(values [option1])
printoption2 is {}format(values [ option2])

gtk 。主要可以嵌套,所以这种模式甚至可以在另一个GUI窗口内工作。从主窗口执行相同的操作(显然名称运行不是特别的,您可能需要将其重命名为更相关的内容,例如get_values或某些东西)。



<对于使用对话框,您也可以这样做,为了在对话窗口中添加小部件,可以引用对话框的vbox属性,即 self.vbox.pack_start(my_checkbox)。通常我会使用一个对话框结合这种技术,所以我不必自己创建按钮。在这种情况下,您可以连接到 ok_button 上单击 / code>对话框( self ),并读取响应代码以知道要返回的内容,通常将对象变量设置为响应值并处理它回来跑步,例如返回None 如果您发现用户点击取消。



编辑:我还应该提到这不只是对于其他窗口,但对于任何您想要阻止代码但不冻结GUI的操作。例如,我将此模式与 gobject.timeout_add 结合使用,以代表代表git克隆的进度条。使用这一点,我能够创建一个单独的函数,它将使用单个for循环克隆 n 存储库,而不是将它们与回调字符串。这是一个非常强大的模式,真的。


I am using PyGTK inside another application to take user input. I am building an window which has lots of Check Buttons and according to response I need to go ahead.

The problem is that in python when I call the function of window and its operation, the program doesn't wait for the response from window? How do I make it able to wait for the response from the window??

I also don't know if I can use a dialog instead of window because I tried adding check buttons to dialog and it did not work out well. If using window is not appropriate than can anybody help me with dialog?

Thanks for your help.

解决方案

(Oldish question, but it deserves an answer)

While pmoleri gave an acceptable answer (which is how I initially handled this case) I find that it is kind of frustrating to add a callback because it obfuscates the program flow. It's neat to have a window that acts like gtk.Dialog, where you can call window.run() and it will block there until you get a response, but the GUI will still run. You can do this functionality yourself with gtk.main():

import gtk
class BlockingWindow(gtk.Window):
    def __init__(self,checkmark_values):
        gtk.Window.__init__(self)
        self.checkmarks = {}
        # Create check marks here, put them in a dictionary
        # relating their name to their widget
        # Also create ok button
        ok_button.connect("clicked",self.on_ok_button_clicked)

    def on_ok_button_clicked(self,button):
        gtk.main_quit()

    def run(self):
        self.show_all()
        gtk.main()
        self.destroy()
        return dict((x,self.checkmarks[x].get_active()) for x in self.checkmarks)

blocking_window = BlockingWindow(["option1","option2"])
values = blocking_window.run()
print "option1 is {}".format(values["option1"])
print "option2 is {}".format(values["option2"])

gtk.main can be nested, so this pattern will work even inside of another GUI window, e.g. doing the same thing from your main window (obviously the name "run" is not special, you may want to rename it something more pertinent, like "get_values" or something).

As for using dialog, you can do that as well, to add widgets to a dialog window, you can reference the dialog's "vbox" attribute, i.e. self.vbox.pack_start(my_checkbox). Usually I'll use a dialog in conjunction with this technique so I don't have to create the buttons myself. In that case, instead of connecting to clicked on ok_button, you can connect to response on the dialog (self) and read the response code to know what you want to return, generally setting an object variable to the response value and handling it back in run, e.g. return None if you find that the user clicked cancel.

Edit: I should also mention that this works not just for other windows, but for any action you want to block your code on but not freeze up the GUI. For instance, I used this pattern in conjunction with gobject.timeout_add to animate a progress bar representing git cloning. Using this, I was able to create a single function that would clone n repositories using a single for loop, instead of stringing them along with callbacks. It's a very powerful pattern, really.

这篇关于使用GTK窗口等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆