gpt4 book ai didi

python - 如何获得 pygtk 窗口的大小?

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:28 26 4
gpt4 key购买 nike

我正在尝试使用 gtk.window.get_size(),但它总是只返回默认的宽度和高度。 The documentation says

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.

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

我正在尝试使用它来保存退出时的窗口大小,以便我可以在启动时恢复它。有更好的方法吗?

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

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

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

最佳答案

这似乎解决了您的问题:

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 信号。如果您在 destroy 信号上执行此操作,就像您描述的那样,它只会返回默认大小。

关于python - 如何获得 pygtk 窗口的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com