开发者

Problems with multiple panels in a single notebook page

开发者 https://www.devze.com 2023-03-03 21:42 出处:网络
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.

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.__i开发者_StackOverflow社区nit__(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()


You've got parenting problems!

For example, you want the widget self.lblRoll1 to be on the panelControl therefore you should make it a child of it.

e.g.

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号