wxPython ScrolledPanel未在对话框中调整大小

时间:2015-11-18 22:00:06

标签: wxpython wxwidgets

首先,我使用wxPython 2.8.12.1。

我尝试创建设置对话框,用户可以在其中查看然后编辑某些设置。 Atm我在滚动面板中有设置然后我希望将面板添加到我的对话框中,使用" Save"和"取消"对话框底部的按钮。我可以模糊地得到我需要的东西,但是滚动的面板开始时非常小(一次甚至不显示整个垂直的文本行),并且它不会随对话框调整大小。这是我的代码:

import wx
import wx.lib.scrolledpanel as scrolled

class SettingsDialog(wx.Dialog):    
    def __init__(self):
        """Initialize the dialog"""
        wx.Dialog.__init__(self, None, wx.ID_ANY, 'Settings', size=(100,100),
                           style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        # Add Settings Panel
        self.panel = SettingsPanel(self)
        self.mainSizer.Add(self.panel, 0, wx.EXPAND)
        # Add Save/Cancel Buttons
        self.createWidgets()
        # Set sizer and window size
        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

    def createWidgets(self):
        """Create and layout the widgets in the dialog"""
        btnSizer = wx.StdDialogButtonSizer()

        saveBtn = wx.Button(self, wx.ID_OK, label="Save")
        saveBtn.Bind(wx.EVT_BUTTON, self.OnSave)
        btnSizer.AddButton(saveBtn)

        cancelBtn = wx.Button(self, wx.ID_CANCEL)
        btnSizer.AddButton(cancelBtn)
        btnSizer.Realize()

        self.mainSizer.Add(btnSizer, 0, wx.ALL | wx.ALIGN_RIGHT, 5)

    def OnSave(self, event):
        pass

class SettingsPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent, -1)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.createWidgets()
        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)
        self.SetupScrolling()

    def createWidgets(self):
        """Create and layout the widgets in the panel"""
        settingsSizer = wx.GridSizer(rows=0, cols=2, hgap=5, vgap=0)

        names = #List of names grabbed from elsewhere
        values = #List of values grabbed from elsewhere
        name_value_dict = zip(names, values)

        for name, value in name_value_dict:
            lbl = wx.StaticText(self, label=''.join([name,':']))
            settingsSizer.Add(lbl, 0, wx.ALL, 5)
            txt = wx.TextCtrl(self, value=value, name="Name")
            settingsSizer.Add(txt, 0, wx.ALL|wx.EXPAND, 5)

        self.mainSizer.Add(settingsSizer, 0, wx.EXPAND)

class MyApp(wx.App):
    def OnInit(self):
        dlg = SettingsDialog()
        dlg.ShowModal()
        dlg.Destroy()
        return True

if __name__ == '__main__':   
    app = MyApp(False)
    app.MainLoop()

任何帮助都会一如既往地受到赞赏。

1 个答案:

答案 0 :(得分:0)

我使用wxPython 3.0版修复了你的代码,它看起来像你想要的那样。它也适用于2.8版本。让我解释一下你应该做些什么。

  1. 让对话框的大小更大。仅显示所有小部件是不够的。 wx.Dialog.__init__(self, None, wx.ID_ANY, 'Settings', size=(300, 300), ...
  2. 为您的size添加ScrolledPanel个参数scrolled.ScrolledPanel.__init__(self, parent, -1, size=(200, 200))
  3. 这就是全部。希望这会有所帮助。