【问题标题】:How to fix the layout stacking in wxpython如何修复 wxpython 中的布局堆叠
【发布时间】:2019-11-13 12:42:34
【问题描述】:

我正在尝试显示 ssh 连接所需的连接设置,我正在使用 wx.BoxSizer 来安排布局,不幸的是布局不起作用并将所有元素堆叠在左上角(直到我通过缩放/最大化来调整窗口大小)。

我已经尝试过使用:self.Update(),self.Refresh() 和 self.Layout() 在我调用 self.Show(True) 方法之后,但这对我不起作用。 如果我删除“状态栏设置”和“创建菜单栏”部分,它可以工作,但我需要这些。

import wx
import connectionSettingsProperties
import connectionSettings
from pubsub import pub

class MyFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self, parent, title = title, size = (1600,800))
        panel = wx.Panel(self)

        #statusbar setup
        self.CreateStatusBar()

        # menu setup
        filemenu = wx.Menu()

        # creating the menubar
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"Menu")
        self.SetMenuBar(menuBar)

        #connectionstatus init
        self.ipLabel = wx.StaticText(panel, label = 'ip:')
        self.usernameLabel = wx.StaticText(panel, label = 'username:')

        #building layout
        vboxMain = wx.BoxSizer(wx.VERTICAL)
        hboxConnection = wx.BoxSizer(wx.HORIZONTAL)
        hboxConnection.Add(self.ipLabel)
        hboxConnection.Add(self.usernameLabel)
        vboxMain.Add(hboxConnection)
        panel.SetSizer(vboxMain)

        #show content
        self.Show(True)

app = wx.App(False)
frame = MyFrame(None, 'MyApp')
app.MainLoop()

这是它最初显示的内容:https://imgur.com/VQebA9t 这就是它的样子:https://imgur.com/60V1tcF 当我重新缩放窗口时,第二个结果就会显示出来。

【问题讨论】:

    标签: python layout wxpython boxsizer


    【解决方案1】:

    你真的很亲密。您应该只添加以下行:

    vboxMain.Fit(panel)
    

    线下:

    panel.SetSizer(vboxMain)
    

    【讨论】:

    • 谢谢,这对我有用!我还找到了一种使用布局功能修复它的方法。我只需要调用 panel.Layout() 而不是 self.Layout()
    • @ErikB。是的,这是另一种方法。但是,您应该阅读docs,因为 Fit() 和 Layout() 在小部件大小的处理方式上表现不同。只是为了避免将来出现意外。
    猜你喜欢
    • 2011-10-29
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多