wxwidgets/wxPython:在保持纵横比的同时调整 wxFrame 的大小

发布于 2024-08-01 23:31:37 字数 165 浏览 5 评论 0原文

我有一个只有一个孩子的 wx.Frame 。 在设置子项的 wx.Sizer 时,我可以使用 wx.SHAPED 标志,它可以保持子项的纵横比锁定。 然而,框架在其纵横比方面仍然具有完全的自由度,并且会在孩子未使用的区域中留下空白区域。

如何在调整大小时锁定 wx.Frame 的纵横比?

I have a wx.Frame that has only a single child. While setting up the child's wx.Sizer, I can use the wx.SHAPED flag, which keeps the aspect ratio of the child locked. However, the Frame still has complete freedom in it's aspect ratio, and will leave a blank space in the area that is left unused by the child.

How can I lock the aspect ratio of the wx.Frame during resizing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-08-08 23:31:37

大小调整器不能应用于顶级窗口(为了定义窗口本身的属性而不是其内容),因此不幸的是没有“真正”的方法来锁定宽高比。 最好的选择是捕获窗口的 OnSize 事件,获取用户想要的窗口大小(存储在 wxSizeEvent 中),计算新的宽度根据宽高比和高度,然后立即使用新尺寸调用 SetSize

这种技术的缺点是在调整大小时会出现视觉伪影(操作系统最初会根据用户拖动窗口的方式绘制窗口,但在调用 SetSize 后立即重新绘制它,从而导致闪烁效果)所以这不是一个很好的解决方案,但是由于您无法将顶级窗口锁定为宽高比(并且根本不需要使用 SetSize 调整大小),因此您必须决定闪烁是否是值得。

Sizers cannot be applied to top-level windows (in order to define properties of the windows themselves as opposed to their contents), so unfortunately there is no "true" way to lock in the aspect ratio. Your best bet would be to catch your window's OnSize event, get the size that the user wants the window to be (which is stored in the wxSizeEvent), calculate the new width and height according to the aspect ratio and then immediately call SetSize with the new size.

This technique has the drawback of visual artifacts during resizing (the operating system will initially paint the window according to how the user has dragged it but then immediately redraw it after you call SetSize, leading to a flickering effect) so it isn't a great solution, however since you cannot lock top-level windows into aspect ratios (and prevent the need to resize with SetSize at all) you'll have to decide whether the flickering is worth it.

蓝天白云 2024-08-08 23:31:37

这是一些示例代码。 有一点闪烁,我不确定有什么好方法可以消除它。

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1, 'Shaped Frame')
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.panel = panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel, 1, wx.SHAPED)
        self.SetSizer(sizer)
    def on_size(self, event):
        event.Skip()
        self.Layout()
        self.panel.SetMinSize(self.panel.GetSize())
        self.Fit()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()

Here is some sample code. There is a good bit of flicker, and I'm not sure of a good way to eliminate it.

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1, 'Shaped Frame')
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.panel = panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel, 1, wx.SHAPED)
        self.SetSizer(sizer)
    def on_size(self, event):
        event.Skip()
        self.Layout()
        self.panel.SetMinSize(self.panel.GetSize())
        self.Fit()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文