wxPython入门指南
我在用wxPython布局我的表单时遇到了一些困难。如果有人能帮我一下,让我知道怎么按照图片中的样子来布局我的表单,我会非常感激。
请分享一些相关的资料。
谢谢!
2 个回答
3
为了好玩,我决定看看自己手动编写类似的东西需要多长时间。大约花了我35分钟左右。不过我跳过了菜单栏。没关系,反正这是我的代码:
import wx
########################################################################
class TabPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
btnOne = wx.Button(self, label="button_10")
btnTwo = wx.Button(self, label="button_11")
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(btnOne, 0, wx.ALIGN_BOTTOM)
sizer.Add(btnTwo, 0, wx.ALIGN_BOTTOM)
self.SetSizer(sizer)
########################################################################
class LeftPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.list_ctrl = wx.ListCtrl(self, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
)
self.list_ctrl.InsertColumn(0, 'Subject')
self.list_ctrl.InsertColumn(1, 'Due')
self.list_ctrl.InsertColumn(2, 'Location', width=125)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 1, wx.EXPAND)
self.SetSizer(sizer)
########################################################################
class RightPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
notebook = wx.Notebook(self)
page1 = TabPanel(notebook)
page2 = TabPanel(notebook)
page3 = TabPanel(notebook)
notebook.AddPage(page1, "Page_1")
notebook.AddPage(page2, "Page_2")
notebook.AddPage(page3, "Page_3")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.EXPAND)
self.SetSizer(sizer)
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
# create row of buttons
for i in range(4):
btn = wx.Button(self, label="button_%s" % i)
btnSizer.Add(btn, 0, wx.ALL, 5)
mainSizer.Add(btnSizer)
# create splitter
splitter = wx.SplitterWindow(self)
leftPanel = LeftPanel(splitter)
rightPanel = RightPanel(splitter)
splitter.SplitVertically(leftPanel, rightPanel)
mainSizer.Add(splitter, 1, wx.EXPAND)
# add second row of buttons
btnSizer2 = wx.BoxSizer(wx.HORIZONTAL)
btn5 = wx.Button(self, label="button_5")
btn6 = wx.Button(self, label="button_6")
btn7 = wx.Button(self, label="button_7")
btn8 = wx.Button(self, label="button_8")
btnSizer2.Add(btn5)
btnSizer2.Add(btn6)
btnSizer2.Add((1,1), 1, wx.EXPAND)
btnSizer2.Add(btn7)
btnSizer2.Add(btn8)
mainSizer.Add(btnSizer2)
self.SetSizer(mainSizer)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Sample", size=(600,800))
panel = MainPanel(self)
statusbar = self.CreateStatusBar()
statusbar.SetStatusText("Test")
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
7
在设计复杂的图形界面时,我建议不要手动去调整布局。请使用wxglade,然后再进行手动调整。
这个过程我花了15分钟,虽然有些错误(我没有填充列表控件,抱歉):
这是我遵循的步骤:
1) Add a Frame widget, select a wx.Frame.
2) Customize wx.Frame properties:
Give the Frame a MenuBar (the property dialog for the menubar appears).
Customize MenuBar: Add File, Edit...
Go back to the Frame properties' View (click the Frame icon on the wxglade Tree View).
(not nice but it's the way I found to go in and out from Menu and StatusBar dialogs).
Give the wx.Frame a StatusBar. Go back (click the Frame icon in the wxglade Tree View).
3) Create 3 vertical slots from the default BoxSizer (right-click on the sizer + add * 3)
1st slot:
Add a BoxSizer with 5 horizontal slots
Set proportion=0 to the sizer, set border=5 and mark wxALL
Add 4 Buttons and a Spacer in those slots
Mark EXPAND and set proportion=1 in the Spacer properties.
2nd slot:
Add a SplitterWindow. Select Vertical type.
Optionally set border=5 with wxALL
Add a BoxSizer to each section of the SplitterWindow
At its left, add a ListCtrl
At its right, add a NoteBook
Customize NoteBook:
Add 4 sheets.
Add BoxSizer with 2 vertical slots to first notebook sheet.
1st slot: insert a wx.Panel.
2nd slot: add a BoxSizer with 3 horizontal slots.
Set proportion=0 to the sizer.
Add 2 buttons and 1 Spacer in those slots.
Mark EXPAND and set proportion=1 in the Spacer properties.
3rd slot:
Add a BoxSizer with 5 horizontal slots.
Set proportion=0 to the sizer, set border=5 with wxALL
Add 2 Buttons, 1 Spacer and 2 Buttons more.
Mark EXPAND and set proportion=1 in the Spacer properties.
4) Give background colour to the Frame.
(Choose wisely, you still have 5 minutes left of the 15 allotted).
最后,记得自定义标签、布局和控件的名称等等。
保存wxGlade文件以便将来修改,并生成Python文件(在顶部的应用程序对话框中)。
小窍门:不要修改生成的文件,而是导入并在另一个文件中对你的Frame进行子类化,以编写它的功能。
最后一点:根据我的经验,Linux和Windows在布局和背景方面有一些不同。在Linux中,不要使用默认的Frame的BoxSizer,如第3步所示。相反,应该在上面添加一个wx.Panel,然后在这个Panel上添加一个BoxSizer,接着从这个布局继续构建。