我正在向水平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
我错过了什么?
答案 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
上,因为父级现在将成为面板。