我如何获得pygtk窗口的大小? [英] How do I get the size of a pygtk window?

查看:118
本文介绍了我如何获得pygtk窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用gtk.window.get_size(),但它总是返回默认的宽度和高度。 文档

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


get_size()方法返回一个包含窗口当前宽度和
高度的元组。如果窗口不在屏幕上,它会返回PyGTK将建议窗口管理器的
大小作为初始窗口
的大小。 get_size()方法获得的大小是配置事件中接收到的最后一个大小
,也就是说,PyGTK使用其本地存储的
大小,而不是查询X服务器的大小。因此,如果
调用resize()方法,然后立即调用get_size()
方法,则大小将不会生效。在窗口管理器
处理调整大小请求后,PyGTK通过配置事件接收到
大小已更改的通知,并且窗口
的大小得到更新。

The get_size() method returns a tuple containing the current width and height of the window. If the window is not on-screen, it returns the size PyGTK will suggest to the window manager for the initial window size. The size obtained by the get_size() method is the last size received in a configure event, that is, PyGTK uses its locally-stored size, rather than querying the X server for the size. As a result, if you call the resize() method then immediately call the get_size() method, the size won't have taken effect yet. After the window manager processes the resize request, PyGTK receives notification that the size has changed via a configure event, and the size of the window gets updated.

我已经尝试手动调整窗口大小并等待一分钟左右,但仍然获得默认的宽度和高度。

I've tried resizing the window manually and waiting a minute or so, but I still get the default width and height.

我试图使用它来保存退出时的窗口大小,以便我可以在开始时恢复它。有没有更好的方法来做到这一点?

I'm trying to use this to save the window size on quit so that I can restore it on start. Is there a better way to do this?

这里是我的主要退出代码片段。

Here's the code snipit I have for my main quit.

def on_main_window_destroy(self, widget, data=None):
    if self.view.current_view.layout == 'list':
        self.view.current_view.set_column_order()

    width = self.main_window.get_size()[0]
    height = self.main_window.get_size()[1]
    #test statement
    print (width, height)

    self.prefs.set_size_prefs(width, height)
    self.prefs.set_view_prefs(self.view.current_view.media, self.view.current_view.layout)
    gtk.main_quit()

我想我明白现在发生了什么。这是在摧毁信号内,所以在代码被调用时,窗口已经消失。是否有一个更经典的处理窗口大小调整的方法?我希望避免每次用户调整窗口大小时处理调整大小事件。

I think I understand what's happening now. This is inside the destroy signal, so by the time the code gets called, the window is already gone. Is there a more canonical way of handling window resizing? I was hoping to avoid handling resize events everytime the user resized the window.

推荐答案

这似乎解决了您的问题:

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()

我认为关键是在 delete_event 信号上调用 get_size ,而不是销毁信号。如果您在 destroy 信号上执行此操作,就像您所描述的那样,它只是返回默认大小。

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.

这篇关于我如何获得pygtk窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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