单个笔记本页面中的多个面板出现问题

时间:2011-05-10 04:49:08

标签: python wxpython

我正在创建一个计算D& D分数的程序。我完成了所有后端,现在我想完成GUI。

我在这里要做的是为某些按钮设置静态面板(下一个,上一个,好的,取消等)。专家组没有合作。

我想尝试将其放在右下角(传统上下一个/上一个按钮的位置)。此面板可以放入笔记本sizer或我为sizerMain中的其他所有内容生成的版本step_1

如果您有任何疑问,请与我们联系。我是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()

1 个答案:

答案 0 :(得分:1)

你有育儿问题!

例如,您希望窗口小部件self.lblRoll1位于panelControl上,因此您应该将其设为它的子项。

e.g。

self.lblRoll1 = wx.StaticText(panelControl,label = "0")

这是你的问题 - 整个代码都会出现。

解决这类问题不可或缺的工具是Widget Inspection tool

另外,我建议您将每个面板的代码分解为自己的类(这将是子类wx.Panel)。这将使阅读和维护变得更加容易。