简体   繁体   中英

pygtk hide/destroy doesn't work

I'm trying to open an Dialog and after getting the response back, closing it again. As far as I understood it, .destroy() or .hide() should remove the window from the screen. But the window freezes and stays open. What am I doing wrong?

This is my code:

#!/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")

Try this:

#!/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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM