How do I get the size of a pygtk window?

后端 未结 2 1506
独厮守ぢ
独厮守ぢ 2021-01-06 17:27

I\'m trying to use gtk.window.get_size(), but it always just returns the default width and height. The documentation says

The get_size() method return

相关标签:
2条回答
  • 2021-01-06 18:22

    This seems to fix your problem:

    import gtk
    
    def print_size(widget, data=None):
        print window.get_size()
    
    def delete_event(widget, data=None):
        print window.get_size()
        return False
    
    def destroy(widget, data=None):
        gtk.main_quit()
    
    window = gtk.Window()
    window.connect('delete_event', delete_event)
    window.connect('destroy', destroy)
    
    button = gtk.Button(label='Print size')
    button.connect('clicked', print_size)
    window.add(button)
    
    window.show_all()
    
    gtk.main()
    

    I think the key is calling get_size on the delete_event signal rather than the destroy signal. If you do it on the destroy signal, it's like you describe, it just returns the default size.

    0 讨论(0)
  • 2021-01-06 18:30

    Try running:

    while gtk.events_pending():
        gtk.main_iteration_do(False)
    

    right before calling window.get_size()

    0 讨论(0)
提交回复
热议问题