0

我正在尝试打开一个对话框,并在收到回复后再次关闭它。据我了解, .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")
4

1 回答 1

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();
于 2018-09-03T06:00:50.153 回答