将绘图区域放置在滚动窗口内时防止显示滚动条
我正在使用 Python 和 pyGTK 开发一个项目。我有一个窗口,其唯一目的是显示图像。除非图像太大而不适合屏幕,否则窗口默认情况下不应显示滚动条。
所以我所做的是这样的:
window = gtk.Window()
window.resize(image.size[0], image.size[1])
scrolled = gtk.ScrolledWindow()
scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled.set_shadow_type(gtk.SHADOW_NONE)
area = gtk.DrawingArea()
area.set_size_request(image.size[0], image.size[1])
window.add(scrolled)
scrolled.add_with_viewport(area)
area.show()
scrolled.show()
window.show()
但这并不完全有效。生成的窗口有点太小,因此会显示滚动条。
为了让它工作,我必须将第二行改为:
window.resize(image.size[0] + 2, image.size[1] + 2)
但这很丑陋,而且它不能在所有系统下工作。在我的 Windows 框中,我必须使用 + 3。
我该怎么做才能确保窗口对于图像来说足够大?
I'm working on a project using Python and pyGTK. I have a window whose only purpose is showing an image. Unless the image is too big to fit the screen, the window should show no scrollbars by default.
So what I do is something like this:
window = gtk.Window()
window.resize(image.size[0], image.size[1])
scrolled = gtk.ScrolledWindow()
scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled.set_shadow_type(gtk.SHADOW_NONE)
area = gtk.DrawingArea()
area.set_size_request(image.size[0], image.size[1])
window.add(scrolled)
scrolled.add_with_viewport(area)
area.show()
scrolled.show()
window.show()
But it doesn't quite work. The resulting window is a little too small, and thus, scrollbars show up.
For it to work, I have to thange the second line to this:
window.resize(image.size[0] + 2, image.size[1] + 2)
But that is ugly, and it doesn't work under all systems. In my Windows box I have to use + 3.
What can I do to make sure the window is big enough for the image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出来了:)。
如果将 DrawingArea 放入 ScrolledWindow 中,则事情将无法正常工作。相反,您必须将 DrawingArea 放入视口中,并将视口放入 ScrolledWindow 中。为了方便起见,gtk.ScrolledWindow 提供了一个方法 gtk.ScrolledWindow.add_with_viewport,它会自动执行此操作。问题是该方法生成的视口默认有一个边框,并且该边框的宽度根据系统而变化。另外,(据我所知)无法访问生成的视口(编辑:您可以使用scrolled.get_child())来消除边框。解决方案很简单:您必须手动创建视口。
它在 Arch (GNOME 3)、Windows XP、Windows 7 和 Ubuntu (Unity) 上表现得非常出色。
I figured it out :).
If you put a DrawingArea inside a ScrolledWindow, things just won't work as they should. Instead, you have to put the DrawingArea in a Viewport, and the Viewport in the ScrolledWindow. gtk.ScrolledWindow provides a method, gtk.ScrolledWindow.add_with_viewport, that does this automatically, for convenience's sake. The problem is the viewport generated by that method has a border by default, and that border varies in width depending on the system. Also, there's no way (that I know of) of accessing the generated Viewport (edit: you can use scrolled.get_child()) to get rid of the border. The solution is simple: you have to manually create the Viewport.
It worked like a charm on Arch (GNOME 3), Windows XP, Windows 7 and Ubuntu (Unity).