单个笔记本页面中多个面板的问题
我正在创建一个程序来计算 D&D 分数。我已经完成了所有后端,现在我想完成 GUI。
我在这里想做的是为某些按钮(下一个、上一个、确定、取消等)设置一个静态面板。该小组不合作。
我想尝试将其放在右下角(传统上下一个/上一个按钮所在的位置)。该面板可以放在笔记本 sizer
中,也可以放在我为 step_1
中其他所有内容制作的 sizer sizerMain
中。
如果您有任何疑问,请告诉我。我对 wxPython 很陌生,我希望你能处理我的代码...:)
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class step_1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
# Create initial sizers and panels
## Main sizer, containing both panels
sizerMain = wx.BoxSizer(wx.VERTICAL)
## For the main control area
panelControl = wx.Panel(self,2)
sizerControl = wx.GridBagSizer(hgap = 4,vgap = 4)
## For buttons
panelBtn = wx.Panel(self,1)
sizerBtn = wx.BoxSizer(wx.HORIZONTAL)
# Add widgets
## Main content area
lblTitle = wx.StaticText(self,label = "Pick Scores")
sizerControl.Add(lblTitle,pos = (0,0),
flag = wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM,
border = 5)
btnRoll = wx.Button(self,label = "Roll!")
sizerControl.Add(btnRoll,pos = (0,1),span = (1,5),
flag = wx.EXPAND|wx.ALL,border = 5)
### Radio boxes
#### Radio button tuple
rboxPick = ["Default","Strength","Dexterity","Constitution",
"Intelligence","Wisdom","Charisma"]
self.lblRoll1 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll1,pos = (1,0),flag = wx.ALIGN_CENTER)
self.rboxRoll1 = wx.RadioBox(self,label = "Roll One",choices = rboxPick)
sizerControl.Add(self.rboxRoll1,pos = (1,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll2 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll2,pos = (2,0),flag = wx.ALIGN_CENTER)
self.rboxRoll2 = wx.RadioBox(self,label = "Roll Two",choices = rboxPick)
sizerControl.Add(self.rboxRoll2,pos = (2,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll3 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll3,pos = (3,0),flag = wx.ALIGN_CENTER)
self.rboxRoll3 = wx.RadioBox(self,label = "Roll Three",choices = rboxPick)
sizerControl.Add(self.rboxRoll3,pos = (3,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll4 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll4,pos = (4,0),flag = wx.ALIGN_CENTER)
self.rboxRoll4 = wx.RadioBox(self,label = "Roll Four",choices = rboxPick)
sizerControl.Add(self.rboxRoll4,pos = (4,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll5 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll5,pos = (5,0),flag = wx.ALIGN_CENTER)
self.rboxRoll5 = wx.RadioBox(self,label = "Roll Five",choices = rboxPick)
sizerControl.Add(self.rboxRoll5,pos = (5,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll6 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll6,pos = (6,0),flag = wx.ALIGN_CENTER)
self.rboxRoll6 = wx.RadioBox(self,label = "Roll Six",choices = rboxPick)
sizerControl.Add(self.rboxRoll6,pos = (6,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
### Instructions
self.tcLogger = wx.TextCtrl(self,style = wx.TE_MULTILINE)
sizerControl.Add(self.tcLogger,pos = (7,0),span = (1,6),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 5)
self.tcLogger.AppendText("""Instructions
1. Click the "Roll!" button up top.
- Scores will be placed in the empty slots on the left side.
2. Look at the scores and decide where you want to put them.
3. Click the correct label for each score.
- Make sure you only assign one score to one ability.
4. Click "Assign" to finalize the assignment.""")
## Button area
self.btnPrev = wx.Button(self,label = "Previous",size = (90,28))
self.btnAssign = wx.Button(self,label = "Assign",size = (90,28))
self.btnNext = wx.Button(self,label = "Next",size = (90,28))
sizerBtn.Add(self.btnPrev)
sizerBtn.Add(self.btnAssign)
sizerBtn.Add(self.btnNext,flag = wx.RIGHT|wx.BOTTOM,border = 5)
self.btnNext.Disable()
self.btnPrev.Disable()
# Set and fit sizers, panels, etc.
## Growable rows and columns
sizerControl.AddGrowableCol(1)
sizerControl.AddGrowableRow(7)
## Finalize sizers and panels
panelControl.SetSizerAndFit(sizerControl)
panelBtn.SetSizerAndFit(sizerBtn)
### Final sizer to hold everything
sizerMain.Add(panelControl,2,wx.EXPAND|wx.ALIGN_TOP|wx.ALL,border = 5)
sizerMain.Add(panelBtn,1,wx.EXPAND|wx.ALIGN_BOTTOM|wx.RIGHT,border = 5)
self.SetAutoLayout(True)
self.SetSizerAndFit(sizerMain)
self.Layout()
# Bind events (as needed)
class step_2(wx.Panel):
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
class step_3(wx.Panel):
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
####
# create a button class here for later, don't worry about it now
####
class main_frame(wx.Frame):
"""Main Frame holding the main panel."""
def __init__(self,*args,**kwargs):
wx.Frame.__init__(self,*args,**kwargs)
# Build the menu bar
menuBar = wx.MenuBar()
menuFile = wx.Menu()
menuFileQuit = menuFile.Append(wx.ID_EXIT, text="&Quit")
#self.Bind(wx.EVT_MENU, self.OnQuit,menuFileQuit)
menuBar.Append(menuFile, "&File")
self.SetMenuBar(menuBar)
p = wx.Panel(self)
nb = wx.Notebook(p)
# create the page windows as children of the notebook
nbPage1 = step_1(nb)
nbPage2 = step_2(nb)
nbPage3 = step_3(nb)
# add the pages to the notebook with the label to show on the tab
nb.AddPage(nbPage1,"Page 1")
nb.AddPage(nbPage2,"Page 2")
nb.AddPage(nbPage3,"Page 3")
# finally, put the notebook in a sizer for the panel to manage the
# layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
self.Center()
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = main_frame(None,-1,size = (1000,1000),title = "D&D Charcter Creator")
app.MainLoop()
I am creating a program to calculate D&D scores. I have all the backend done, and I want to get the GUI done now.
What I am trying to do here is have a static panel for certain buttons (next, previous, ok, cancel, etc.). The panel is not cooperating.
I want to try to get it on the bottom right (where next/previous buttons traditionally are). This panel can go in the notebook sizer
or in the sizer sizerMain
I have made for everything else in step_1
.
Let me know if you have any questions. I am very new to wxPython and I hope you can deal with my code... :)
Code is below:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class step_1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
# Create initial sizers and panels
## Main sizer, containing both panels
sizerMain = wx.BoxSizer(wx.VERTICAL)
## For the main control area
panelControl = wx.Panel(self,2)
sizerControl = wx.GridBagSizer(hgap = 4,vgap = 4)
## For buttons
panelBtn = wx.Panel(self,1)
sizerBtn = wx.BoxSizer(wx.HORIZONTAL)
# Add widgets
## Main content area
lblTitle = wx.StaticText(self,label = "Pick Scores")
sizerControl.Add(lblTitle,pos = (0,0),
flag = wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM,
border = 5)
btnRoll = wx.Button(self,label = "Roll!")
sizerControl.Add(btnRoll,pos = (0,1),span = (1,5),
flag = wx.EXPAND|wx.ALL,border = 5)
### Radio boxes
#### Radio button tuple
rboxPick = ["Default","Strength","Dexterity","Constitution",
"Intelligence","Wisdom","Charisma"]
self.lblRoll1 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll1,pos = (1,0),flag = wx.ALIGN_CENTER)
self.rboxRoll1 = wx.RadioBox(self,label = "Roll One",choices = rboxPick)
sizerControl.Add(self.rboxRoll1,pos = (1,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll2 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll2,pos = (2,0),flag = wx.ALIGN_CENTER)
self.rboxRoll2 = wx.RadioBox(self,label = "Roll Two",choices = rboxPick)
sizerControl.Add(self.rboxRoll2,pos = (2,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll3 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll3,pos = (3,0),flag = wx.ALIGN_CENTER)
self.rboxRoll3 = wx.RadioBox(self,label = "Roll Three",choices = rboxPick)
sizerControl.Add(self.rboxRoll3,pos = (3,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll4 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll4,pos = (4,0),flag = wx.ALIGN_CENTER)
self.rboxRoll4 = wx.RadioBox(self,label = "Roll Four",choices = rboxPick)
sizerControl.Add(self.rboxRoll4,pos = (4,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll5 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll5,pos = (5,0),flag = wx.ALIGN_CENTER)
self.rboxRoll5 = wx.RadioBox(self,label = "Roll Five",choices = rboxPick)
sizerControl.Add(self.rboxRoll5,pos = (5,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.lblRoll6 = wx.StaticText(self,label = "0")
sizerControl.Add(self.lblRoll6,pos = (6,0),flag = wx.ALIGN_CENTER)
self.rboxRoll6 = wx.RadioBox(self,label = "Roll Six",choices = rboxPick)
sizerControl.Add(self.rboxRoll6,pos = (6,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
### Instructions
self.tcLogger = wx.TextCtrl(self,style = wx.TE_MULTILINE)
sizerControl.Add(self.tcLogger,pos = (7,0),span = (1,6),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 5)
self.tcLogger.AppendText("""Instructions
1. Click the "Roll!" button up top.
- Scores will be placed in the empty slots on the left side.
2. Look at the scores and decide where you want to put them.
3. Click the correct label for each score.
- Make sure you only assign one score to one ability.
4. Click "Assign" to finalize the assignment.""")
## Button area
self.btnPrev = wx.Button(self,label = "Previous",size = (90,28))
self.btnAssign = wx.Button(self,label = "Assign",size = (90,28))
self.btnNext = wx.Button(self,label = "Next",size = (90,28))
sizerBtn.Add(self.btnPrev)
sizerBtn.Add(self.btnAssign)
sizerBtn.Add(self.btnNext,flag = wx.RIGHT|wx.BOTTOM,border = 5)
self.btnNext.Disable()
self.btnPrev.Disable()
# Set and fit sizers, panels, etc.
## Growable rows and columns
sizerControl.AddGrowableCol(1)
sizerControl.AddGrowableRow(7)
## Finalize sizers and panels
panelControl.SetSizerAndFit(sizerControl)
panelBtn.SetSizerAndFit(sizerBtn)
### Final sizer to hold everything
sizerMain.Add(panelControl,2,wx.EXPAND|wx.ALIGN_TOP|wx.ALL,border = 5)
sizerMain.Add(panelBtn,1,wx.EXPAND|wx.ALIGN_BOTTOM|wx.RIGHT,border = 5)
self.SetAutoLayout(True)
self.SetSizerAndFit(sizerMain)
self.Layout()
# Bind events (as needed)
class step_2(wx.Panel):
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
class step_3(wx.Panel):
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
####
# create a button class here for later, don't worry about it now
####
class main_frame(wx.Frame):
"""Main Frame holding the main panel."""
def __init__(self,*args,**kwargs):
wx.Frame.__init__(self,*args,**kwargs)
# Build the menu bar
menuBar = wx.MenuBar()
menuFile = wx.Menu()
menuFileQuit = menuFile.Append(wx.ID_EXIT, text="&Quit")
#self.Bind(wx.EVT_MENU, self.OnQuit,menuFileQuit)
menuBar.Append(menuFile, "&File")
self.SetMenuBar(menuBar)
p = wx.Panel(self)
nb = wx.Notebook(p)
# create the page windows as children of the notebook
nbPage1 = step_1(nb)
nbPage2 = step_2(nb)
nbPage3 = step_3(nb)
# add the pages to the notebook with the label to show on the tab
nb.AddPage(nbPage1,"Page 1")
nb.AddPage(nbPage2,"Page 2")
nb.AddPage(nbPage3,"Page 3")
# finally, put the notebook in a sizer for the panel to manage the
# layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
self.Center()
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = main_frame(None,-1,size = (1000,1000),title = "D&D Charcter Creator")
app.MainLoop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的育儿方式有问题!
例如,您希望小部件
self.lblRoll1
位于panelControl
上,因此您应该将其设为它的子级。例如
这是您的问题 - 它出现在您的整个代码中。
解决此类问题不可或缺的工具是Widget 检查工具。
另外,我建议您将每个面板的代码分解到它自己的类中(这将是 wx.Panel 的子类)。这将使一切更容易阅读和维护。
You've got parenting problems!
For example, you want the widget
self.lblRoll1
to be on thepanelControl
therefore you should make it a child of it.e.g.
This is your problem -it occurs throughout your code.
An indispensable tool for solving these type of issues is the Widget Inspection tool.
Also Id advise you to factor out the code for each panel into its own class (which would subclass wx.Panel). This will make it all much easier to read and maintain.