wxpython sizer 错误

发布于 2024-12-14 08:37:26 字数 273 浏览 0 评论 0原文

我不知道应该如何调整这个大小,我一直在尝试使用 sizer 但似乎我一直在做错事。

基本上我有这个代码:

self.button = wx.Button(self, label="Create New", pos=(25,250))

我想使用它的大小调整器。所以它会始终将其位置调整到窗口。初始起始位置为25、250。初始窗口高度为400。 我希望按钮能够根据窗口的变化进行相应调整。

谢谢,如果您有任何疑问或需要澄清,请发表评论。

I am not sure how I should size this, I have been trying to use sizer but it seems i am doing something wrong all the time.

Basically i have this code:

self.button = wx.Button(self, label="Create New", pos=(25,250))

and i would like to use a sizer with it. So it will adjust its position always to the window. The initial starting position is 25, 250. The height of the initial window is 400.
I would like the button to adjust accordingly to the window changes.

Thank you, please comment if you have any questions or need clarification.

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

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

发布评论

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

评论(1

陪我终i 2024-12-21 08:37:26

这里有一个 wxglade 自动生成的代码,展示了如何使用 wx.BoxSizer
它只是一个带有面板小部件的框架。您可以使用按钮更改面板,它将完美运行。这个想法是,您可以添加其他小部件,就像在 wx.Panel 中所做的那样:

sizer_2.Add(self.panel_1, 1, wx.EXPAND, 0)

这会使您的小部件位于一行中。如果您想要其他配置,您可以使用不同的 sizer,您还可以将带有小部件的 sizer 添加到其他 sizer。 sizer.Add() 中的参数确定 sizer 的边框和行为。您应该检查 wxPython/wxglade 文档。不管怎样,结构总是像下面这样:

import wx

# begin wxGlade: extracode
# end wxGlade

class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.CLOSE_BOX|wx.SYSTEM_MENU| wx.CAPTION
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)
        ------ create other widgets here ------

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle("frame_2")
        self.SetMinSize((300,200))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.panel_1, 1, wx.EXPAND, 0)
        ------ add other widgets or even sizers to the sizer here ------
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()
        # end wxGlade

# end of class MyFrame1

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame1(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

Here you have a wxglade automatically-generated code showing how to use a wx.BoxSizer.
It is simply a Frame with a panel widget. You can change the panel with your button and it will work perfectly. The idea is that you could add other widgets as it is done for the wx.Panel in:

sizer_2.Add(self.panel_1, 1, wx.EXPAND, 0)

this produce your widgets being situated in a row. If you want another disposition you could use a different sizer, You can also add sizers with widgets to other sizers. The parameters in sizer.Add() determine the borders and behavior of the sizer. You should check wxPython/wxglade documentation for that. Anyway, the estructure is always like the one below:

import wx

# begin wxGlade: extracode
# end wxGlade

class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.CLOSE_BOX|wx.SYSTEM_MENU| wx.CAPTION
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)
        ------ create other widgets here ------

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle("frame_2")
        self.SetMinSize((300,200))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.panel_1, 1, wx.EXPAND, 0)
        ------ add other widgets or even sizers to the sizer here ------
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()
        # end wxGlade

# end of class MyFrame1

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