pygtk hide / destroy不起作用

时间:2013-11-14 23:25:15

标签: python window pygtk

我正在尝试打开一个Dialog,在收到回复之后再次关闭它。 据我所知,.destroy()或.hide()应该从屏幕上删除窗口。但窗户冻结并保持打开状态。我做错了什么?

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk
import time

class MyWindow(gtk.Dialog):

    def __init__(self, parent=None, filename="Filename.jpg"):
        gtk.Dialog.__init__(self, "Window", parent, 0,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
             gtk.STOCK_OK, gtk.RESPONSE_OK))

        self.set_default_size(150, 100)
        box = self.get_content_area()

        self.usr_label = gtk.Label("User-Name:\t")
        self.usr_entry = gtk.Entry()
        box.add(self.usr_label)
        box.add(self.usr_entry)

        self.show_all()


dialog = MyWindow(filename="foobar")
response = dialog.run()
if response != gtk.RESPONSE_OK:
       exit()
dialog.hide()
dialog.destroy()
time.sleep(15)
print("foo")

1 个答案:

答案 0 :(得分:0)

尝试一下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk, time;

class MyWindow(gtk.Dialog):
    def __init__(self, parent=None, filename="Filename.jpg"):
        gtk.Dialog.__init__(self, "Window", parent, 0,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                gtk.STOCK_OK, gtk.RESPONSE_OK));

        self.connect("destroy", self.quit);
        self.connect("response", self.response);

        self.set_default_size(150, 100)
        box = self.get_content_area()

        self.usr_label = gtk.Label("User-Name:\t")
        self.usr_entry = gtk.Entry()
        box.add(self.usr_label)
        box.add(self.usr_entry)

        self.show_all();

    def response(self, widget = None, data = None):
        if data == gtk.RESPONSE_OK:
            name = self.usr_entry.get_text();
            print("User-Name: " + name);
            self.quit();
        else:
            self.quit()

    def main(self): gtk.main();
    def quit(self, widget = None): gtk.main_quit();

if __name__ == '__main__': MyWindow().main();