wxPython BoxSizer中重叠的小部件(控件)

时间:2018-04-19 13:15:13

标签: python wxpython boxsizer

我正在向水平wx.BoxSizer中添加元素,但它们不是彼此相邻布局,而是彼此重叠显示(所有这些都放在父级的像素位置(0,0)中面板)。

以下是文件的缩短版本(相关部分):

main.py:

import wx
from frm_users import UsersForm

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "My App Title", size=(1200, 800))
        self.panel = wx.Panel(self, wx.ID_ANY)
        self.statusbar = self.CreateStatusBar(2)
        self.statusbar.SetStatusWidths([-1, 60])

    # this event is bound to a menu item I construct elsewhere
    def onUsers(self, event=None):
        frmUsers = UsersForm(self.panel)


if __name__ == "__main__":    
    app = wx.App(False)
    frame = MainFrame()    
    frame.Show()
    app.MainLoop()

frm_users.py:

from dbmodel import OlvUsers, Users
import forms_controller
import wx

class UsersForm(wx.Panel):
    def __init__(self, parent):
        # parent here is the panel

        toolbox = forms_controller.getToolboxSizer(self, parent)
        parent.SetSizer(toolbox)


    def onSearch(self, event=None):
        print("Searching")

forms_controller.py:

import wx


def getToolboxSizer(parent, frame):
    # frame is a panel usually
    toolboxSizer = wx.BoxSizer(wx.HORIZONTAL)
    toolboxSizer.AddSpacer(5)

    font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)
    # create the search related widgets
    searchByLbl = wx.StaticText(frame, label="Search By:")
    searchByLbl.SetFont(font)
    toolboxSizer.Add(searchByLbl, 0, wx.ALL, 5)

    cat = ["Author", "Title", "ISBN", "Publisher"]
    categories = wx.ComboBox(frame, value="Author", choices=cat)
    toolboxSizer.Add(categories, 0, wx.ALL, 5)

    search = wx.SearchCtrl(frame, style=wx.TE_PROCESS_ENTER)
    search.Bind(wx.EVT_TEXT_ENTER, parent.onSearch)
    toolboxSizer.Add(search, 0, wx.ALL, 5)

    return toolboxSizer

我错过了什么?

1 个答案:

答案 0 :(得分:0)

main.py中,UsersForm的实例不在sizer内。面板不会自动填充其父级,除非它们是wx.Frame的唯一子级。要使其工作,您应该将frmUsers添加到与框架中的面板关联的sizer中。

我认为getToolboxSizer中可能还存在一个问题,因为小部件似乎直接添加到框架而不是面板。您通常希望将子窗口小部件添加到Panel,以便Tab键可以正常工作。

我可能会将onUsers更改为以下内容:

def onUsers(self, event=None):
    child_sizer = getToolboxSizer(self.panel)
    self.main_sizer.Add(child_sizer, 0, wx.ALL, 5)

然后更新getToolboxSizer,因此它不会将所有小部件放在parent上,因为父级现在将成为面板。