Problems with multiple panels in a single notebook page - python

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()

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.

Related

wx python mysteriously adding a newline character to GUI output

so the goal here is to take an input from the user and display relevant data from an excel sheet.
Our problem is a rather minor, but irritating one...
When the input doesn't get a match from the database, we will display a message saying that the input number was invalid. We used a helper function to format the output to look like this
Here's the formatting code:
def makeRMAString(self, RMAnum, DMRnum, serial, status, cdate, udate):
rma_string = 'RMA#\t: {}\nDMR#\t: {} \nSerial#\t: {}\nStatus\t: {}\nCreated\t: {}\nUpdate\t: {}'
return rma_string.format(RMAnum, DMRnum, serial, status, cdate, udate)
Screenshot shows that all is well when the input matches something in the database. When it doesn't, we execute the following line:
self.rmaData.Label = self.makeRMAString('Invalid RMA number', '' , '', '', '', '')
And this is where the issue occurs; instead of having a nicely formatted output, the 'Invalid RMA number' gets pushed into the next line as if there was a nextline added in:
like this
I launched from command prompt to see the output, and it looks just fine there. Here's proof
We are using wxpython's BoxSizer to help:
self.rmaData = wx.StaticText(self.panel, wx.ID_ANY, self.makeRMAString('','','','','',''))
self.sizer_rmaHoriz = wx.BoxSizer(wx.HORIZONTAL)
self.sizer_rmaHoriz.Add(self.rmaData, 4, wx.ALIGN_LEFT|wx.CENTER, 15)
self.rmaData.SetFont(font)
We are using python 2.7.10
As requested a demo of a Grid sizer.
I've chosen a GridBagSizer, it seems the most appropriate to your requirement.
import wx
data = [
["99550","539761","016833-0695","Shipped","03/27/20","Unknown"],
["99551","539761","xxxxxx-xxxx","Shipped","03/22/20","Unknown"],
["99554","539761","016833-0695","Warehouse 1","01/12/19","Unknown"],
['Invalid RMA number','','','','','',''],
["99555","539761","777777-0695","Warehouse 2","02/27/20","Unknown"],
["99556","539761","016833-XXXX","Shipped","03/27/20","03/24/20"],
['Invalid RMA number','','','','','','']
]
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title = title)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.idx = 0
panel = wx.Panel(self)
sizer = wx.GridBagSizer(0,0)
self.rma = wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
sizer.Add(self.rma, pos = (0, 1), flag = wx.ALIGN_CENTRE_VERTICAL)
EnterB = wx.Button(panel, label = "Enter RMA")
sizer.Add(EnterB, pos = (0,2),flag = wx.ALL, border = 5)
text = wx.StaticText(panel, label = "RMA :")
sizer.Add(text, pos = (1, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc = wx.TextCtrl(panel, style=wx.TE_READONLY, size=(150,-1))
sizer.Add(self.tc, pos = (1, 1), flag = wx.EXPAND)
text1 = wx.StaticText(panel, label = "DMR :")
sizer.Add(text1, pos = (2, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc1 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc1, pos = (2,1), flag = wx.EXPAND)
text2 = wx.StaticText(panel,label = "Serial :")
sizer.Add(text2, pos = (3, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc2 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc2, pos = (3,1), flag = wx.EXPAND)
text3 = wx.StaticText(panel,label = "Status :")
sizer.Add(text3, pos = (4, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc3 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc3, pos = (4,1),flag = wx.EXPAND)
text4 = wx.StaticText(panel,label = "Created:")
sizer.Add(text4, pos = (5, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
self.tc4 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc4, pos = (5,1), flag = wx.EXPAND)
text5 = wx.StaticText(panel,label = "Update :")
sizer.Add(text5, pos = (6, 0), flag = wx.ALIGN_CENTRE_VERTICAL)
self.tc5 = wx.TextCtrl(panel, style=wx.TE_READONLY)
sizer.Add(self.tc5, pos = (6,1), flag = wx.EXPAND)
Next = wx.Button(panel, label = "Demo")
sizer.Add(Next, pos = (7, 1))
panel.SetSizerAndFit(sizer)
EnterB.Bind(wx.EVT_BUTTON, self.OnRma)
self.rma.Bind(wx.EVT_TEXT_ENTER, self.OnRma)
Next.Bind(wx.EVT_BUTTON, self.OnNext)
def OnRma(self, event):
rma = self.rma.GetValue()
idx = [i for i, e in enumerate(data) if e[0] == rma]
if idx:
self.idx = idx[0]
self.OnNext(None)
else:
self.tc.SetValue('Invalid RMA number')
self.tc1.SetValue('')
self.tc2.SetValue('')
self.tc3.SetValue('')
self.tc4.SetValue('')
self.tc5.SetValue('')
def OnNext(self, event):
self.rma.SetValue('')
self.tc.SetValue(data[self.idx][0])
self.tc1.SetValue(data[self.idx][1])
self.tc2.SetValue(data[self.idx][2])
self.tc3.SetValue(data[self.idx][3])
self.tc4.SetValue(data[self.idx][4])
self.tc5.SetValue(data[self.idx][5])
self.idx += 1
if self.idx > len(data) - 1:
self.idx = 0
app = wx.App()
Example(None, title = 'GridBag Demo')
app.MainLoop()
Useage:
input an RMA number (99550, 99551, 99554, 99555 or 99556) and press Enter or the Enter Rma button
enter some invalid RMA and press Enter or the Enter Rma button
Click the Demo button, it will scroll through the data

wxPython -- BoxSizers not placing things correctly

I can't figure out what I'm doing wrong. I just made the jump from Tkinter to wxPython and I'm trying to figure out BoxSizers. I'd look this question up, but I don't even know what to look up. This panel is filling the space of a Frame, it's supposed to show a line of text with a progressbar underneath it and that's all supposed to take up the bottom 1/5 of the panel or so, centered horizontally (eventually I'm going to add a background image behind it). But what happens is I only see the text and only about 40% down from the top, aligned to the left edge of the window. Here's the code:
class KhPanel(wx.Panel):
def __init__(self, parent, configSet, selectWindow):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.configSet = configSet
whichWindow = getattr(self, selectWindow)
whichWindow()
def configWindow(self):
gaugeWidth = (1/5)*self.configSet["width"]
gaugeHeight = (1/10)*self.configSet["height"]
gaugeMax = 100
topBuffer = (8/10)*self.configSet["height"]
itemSep = (1/16)*self.configSet["height"]
vSizer = wx.BoxSizer(wx.VERTICAL)
textSizer = wx.BoxSizer(wx.HORIZONTAL)
progressSizer = wx.BoxSizer(wx.HORIZONTAL)
configText = wx.StaticText(self, label="STUFF", style=wx.ALIGN_CENTER)
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
textSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
progressSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 1)
vSizer.Add(textSizer, 1, wx.TOP, topBuffer)
vSizer.Add(progressSizer, 1, wx.TOP, itemSep)
self.SetSizer(vSizer)
vSizer.Fit(self)
return
If you need the info, configSet.width and height are the width and height of the parent window (currently 340 x 270). And selectWindow, in this case, is "configWindow"
Running this code, the gaugeWidth and gaugeHeight are both getting set to zero, which is why the progressbar is not showing. This is due to the fact that you are doing integer math here, so 1 divided by 5 is 0. Same with 1/10. Just change those lines to:
gaugeWidth = (1/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
Then the gauge will appear. Here's some fully runnable code, slightly modified from your unrunnable original:
import wx
class KhPanel(wx.Panel):
def __init__(self, parent, configSet):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.configSet = configSet
self.configWindow()
def configWindow(self):
gaugeWidth = (1/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
gaugeMax = 100
topBuffer = (8/10)*self.configSet["height"]
itemSep = (1/16)*self.configSet["height"]
vSizer = wx.BoxSizer(wx.VERTICAL)
textSizer = wx.BoxSizer(wx.HORIZONTAL)
progressSizer = wx.BoxSizer(wx.HORIZONTAL)
configText = wx.StaticText(self, label="STUFF", style=wx.ALIGN_CENTER)
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
textSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
progressSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 1)
vSizer.Add(textSizer, 1, wx.TOP, topBuffer)
vSizer.Add(progressSizer, 1, wx.TOP, itemSep)
self.SetSizer(vSizer)
vSizer.Fit(self)
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Test")
config = {'width':340, 'height':270}
panel = KhPanel(self, config)
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
app.MainLoop()
Incorporating Mike Driscoll's correction above, I changed the way I spaced it and it works out to be less work. I took out the horizontal sizers as I discovered they weren't needed unless I was going to space two things on the same row and I added stretch spacers instead of enormous borders. Here's the new code and it looks exactly how I wanted it:
def configWindow(self):
gaugeWidth = (4/5.0)*self.configSet["width"]
gaugeHeight = (1/10.0)*self.configSet["height"]
gaugeMax = 100
vSizer = wx.BoxSizer(wx.VERTICAL)
configText = wx.StaticText(self, label="STUFF")
configProgressBar = wx.Gauge(self, range=gaugeMax, size=(gaugeWidth, gaugeHeight))
vSizer.AddStretchSpacer(7)
vSizer.Add(configText, 1, wx.ALIGN_CENTER, 0)
vSizer.Add(configProgressBar, 1, wx.ALIGN_CENTER, 0)
vSizer.AddStretchSpacer(1)
self.SetSizer(vSizer)
return

WxPython dynamically added sizers mis-behaving

I'm trying to set-up a display to show the gamertag and avatar of users added to a text file, it most of the way there but I can't get them to position properly.
A quick mock-up of what I want: here.
Here is what I currently have on start: here
EDIT: I've switched from using a BoxSizer to using a GridSizer and that seems to have fixed the position issue, they no longer overlap, the shifting problem is still present however.
The sizer containing the users shouldn't be overlapping with the input sizer at the top, I don't know what is causing this.
And what happens when it updates to check for new users: here
Might not be that easy to see but in the second image the lowest user is shifted down, it gets further and further down as the program runs, each time it is moved down by it's own height.
The relevant code areas:
Creating the starting sizers
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.widget_sizer = wx.BoxSizer(wx.VERTICAL)
#Holds input for gamertags and addition
self.input_sizer = wx.BoxSizer(wx.HORIZONTAL)
#Content to be added immediately.
self.gamer_tag_textbox = wx.TextCtrl(self, -1)
self.gamer_tag_textbox.SetFocus()
self.add_gamer_tag = wx.Button(self, -1, 'Add Friend')
#Contains the displayed content
self.user_sizer = wx.BoxSizer(wx.VERTICAL)
#Add objects to sizers
self.input_sizer.Add(self.gamer_tag_textbox, 0)
self.input_sizer.Add(self.add_gamer_tag, 0)
#Set up the sizers
self.widget_sizer.Add(self.input_sizer, 0)
self.widget_sizer.Add(self.user_sizer, 0)
self.main_sizer.Add(self.widget_sizer, 0)
self.SetSizer(self.main_sizer)
Adding sizers created for each user to the main user_sizer.
def display_user_content(self, details):
self.user_sizer.Clear(True)
#This is different to the original code, it originally used boxsizers in the for each loop.
self.single_user_sizer = wx.GridSizer(cols=2)
for each in details:
#Create sizer to contain user information
#Get username
username = each[0]
#Get location of image file
location = each[-1]
#Create static text to contain username
stat = wx.StaticText(self, -1, 'username')
#Load image from location and convert to bitmap.
png = wx.Image(location, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#Create bitmap
avatar = wx.StaticBitmap(self, -1, png)
#Add to sizer
self.single_user_sizer.Add(avatar, 1)
self.single_user_sizer.Add(stat, 1)
#Add each users sizer to main user sizer
self.user_sizer.Add(self.single_user_sizer, 1)
#Add main user sizer to widget sizer
self.widget_sizer.Add(self.user_sizer, 0)
self.frame.Fit()
Full code (minus classes): here
Maybe this is similar to what you would like to achieve?
import wx
NUMBER = 3
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)
self.sizer = wx.GridBagSizer(vgap=5, hgap=5)
self.text = wx.TextCtrl(self.panel, size=(0, 0))
self.button = wx.Button(self.panel)
self.sizer.Add(self.text, (0, 0), flag=wx.EXPAND)
self.sizer.Add(self.button, (0, 1))
self.icons = []
self.stats = []
for i in range(NUMBER):
icon = wx.Panel(self.panel, size=(50, 50))
icon.SetBackgroundColour(wx.RED)
stat = wx.Panel(self.panel, size=(200, -1))
stat.SetBackgroundColour(wx.BLUE)
self.sizer.Add(icon, (i+1, 0))
self.sizer.Add(stat, (i+1, 1), flag=wx.EXPAND)
self.icons.append(icon)
self.stats.append(stat)
self.sizer.AddGrowableCol(1)
self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.border)
self.SetSizerAndFit(self.windowSizer)
self.Show()
app = wx.App(False)
win1 = MainWindow(None)
app.MainLoop()
Or maybe more like this?
import wx
NUMBER = 3
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)
self.sizer = wx.GridBagSizer(vgap=5, hgap=5)
self.toolbar_sizer = wx.BoxSizer()
self.text = wx.TextCtrl(self.panel)
self.button = wx.Button(self.panel)
self.toolbar_sizer.Add(self.text, 0, flag=wx.CENTER)
self.toolbar_sizer.Add(self.button, 0)
self.sizer.Add(self.toolbar_sizer, (0, 0), span=(1, 2), flag=wx.EXPAND)
self.icons = []
self.stats = []
for i in range(NUMBER):
icon = wx.Panel(self.panel, size=(50, 50))
icon.SetBackgroundColour(wx.RED)
stat = wx.Panel(self.panel, size=(200, -1))
stat.SetBackgroundColour(wx.BLUE)
self.sizer.Add(icon, (i+1, 0))
self.sizer.Add(stat, (i+1, 1), flag=wx.EXPAND)
self.icons.append(icon)
self.stats.append(stat)
self.sizer.AddGrowableCol(1)
self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.border)
self.SetSizerAndFit(self.windowSizer)
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
At the end of the display_user_content function I was adding the user_sizer to the widget_sizer each time, this was unnecessary and was causing a doubling of the number of results, I have removed that line and my code now works.
The fixed code:
def display_user_content(self, details):
self.user_sizer.Clear(True)
self.single_user_sizer = wx.GridSizer(cols=2, hgap=5, vgap=5)
for each in details:
#Get username
self.username_sizer = wx.BoxSizer(wx.HORIZONTAL)
username = each[0]
#Get location of image file
location = each[-1]
#Create static text to contain username
stat = wx.StaticText(self, -1, 'username')
#Load image from location and convert to bitmap.
png = wx.Image(location, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#Create bitmap
avatar = wx.StaticBitmap(self, -1, png)
#Add to sizer
self.single_user_sizer.Add(avatar, 0)
self.username_sizer.Add(stat, 0)
self.single_user_sizer.Add(self.username_sizer, 0)
#Add each users sizer to main user sizer
self.user_sizer.Add(self.single_user_sizer, 0)
#Add main user sizer to widget sizer
#self.widget_sizer.Add(self.user_sizer, 0)
self.Fit()

Destroy() wxpython simple error?

i am having an interesting problem,
This program is a simple image viewer and it can contain different images in a listbox. The listbox contains the names of the images. You can load an image to an item in the listbox. You can click any item on the listbox to see its image. For some reason Destroy() is not functioning properly. Please run the following code if you are unable to understand me,
IMAGE_NAME=[]
IMAGE_DATA=[]
IMAGE_LISTSEL=[]
import sys
import wx
def deletepic(self,parent):
try:
bitmap1.Destroy()
bmp1.Destroy()
except:
print sys.exc_info()
def sendnewpic(self,parent):
global scroll_img
deletepic(self,parent)
print IMAGE_DATA[IMAGE_LISTSEL[0]]
if IMAGE_DATA[IMAGE_LISTSEL[0]]!='':
try:
print IMAGE_DATA[IMAGE_LISTSEL[0]]
bmp1 = wx.Image(IMAGE_DATA[IMAGE_LISTSEL[0]], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bitmap1 = wx.StaticBitmap(scroll_img, -1, bmp1, (0, 0))
except:
pass
def areachange(self,pg):
print pg
try:
if IMAGE_DATA[IMAGE_LISTSEL[0]]=='':
deletepic(self,parent)
except:
pass
if pg=="Images":
self.images_area.Show()
else:
self.images_area.Hide()
class imageMax(wx.Panel):
pass
class imageTab(imageMax):
def imagesel(self,parent):
IMAGE_LISTSEL[:] = []
IMAGE_LISTSEL.append(self.listBox.GetSelection())
sendnewpic(self,parent)
def newAddImage(self,parent):
IMAGE_NAME.append('hi');
IMAGE_DATA.append('');
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(len(IMAGE_NAME)-1)
self.imagesel(self) #making it a selected image, globally
def reName(self,parent):
sel = self.listBox.GetSelection()
text = self.listBox.GetString(sel)
renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
if renamed != '':
IMAGE_NAME.pop(sel)
IMAGE_NAME.insert(sel,renamed)
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(sel)
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.listBox = wx.ListBox(self, size=(200, -1), choices=IMAGE_NAME, style=wx.LB_SINGLE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.VERTICAL) #change to horizontal for side by side
self.sizerMain = wx.BoxSizer()
self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
self.listBox.Bind(wx.EVT_LISTBOX, self.imagesel)
btn = wx.Button(self, label="Create New",size=(200, 40))
btnTwo = wx.Button(self, label="Test 2",size=(200, 40))
btn.Bind(wx.EVT_BUTTON, self.newAddImage)
self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
btnSizer.Add(btn, 0, wx.ALL, 5)
btnSizer.Add(btnTwo, 0, wx.ALL, 5)
self.sizer.Add(btnSizer)
self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=0)
self.SetSizer(self.sizerMain)
class MyNotebook(wx.Notebook):
def __init__(self, *args, **kwargs):
wx.Notebook.__init__(self, *args, **kwargs)
class MyPanel(imageTab):
def OnClickTop(self, event):
scroll_img.Scroll(600, 400)
def OnClickBottom(self, event):
scroll_img.Scroll(1, 1)
def OnPageChanged(self, event):
new = event.GetSelection()
areachange(self,self.notebook.GetPageText(new))
event.Skip()
def OnPageChanging(self, event):
event.Skip()
def onOpenFile(self,parent):
""" Open a file"""
filename = wx.FileSelector()
if (filename!=''):
global bitmap1,bmp1,scroll_img
if IMAGE_DATA[IMAGE_LISTSEL[0]]!='':
deletepic(self,parent)
bmp1 = wx.Image(filename, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bitmap1 = wx.StaticBitmap(scroll_img, -1, bmp1, (0, 0))
scroll_img.SetScrollbars(1, 1, bmp1.GetWidth(), bmp1.GetHeight())
IMAGE_DATA[IMAGE_LISTSEL[0]]=filename
print IMAGE_DATA
def __init__(self, *args, **kwargs):
global bitmap1,bmp1,scroll_img
wx.Panel.__init__(self, *args, **kwargs)
self.notebook = MyNotebook(self, size=(225, -1))
# self.button = wx.Button(self, label="Something else here? Maybe!")
tab_images = imageTab(self.notebook)
# add the pages to the notebook with the label to show on the tab
self.notebook.AddPage(tab_images, "Pics",select=True)
scroll_img = wx.ScrolledWindow(self, -1)
scroll_img.SetScrollbars(1, 1, 600, 400)
#self.button = wx.Button(scroll_img, -1, "Scroll Me", pos=(50, 20))
#self.Bind(wx.EVT_BUTTON, self.OnClickTop, self.button)
#self.button2 = wx.Button(scroll_img, -1, "Scroll Back", pos=(500, 350))
#self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
self.images_area=wx.StaticBox(self, -1, '')
self.sizerBox = wx.StaticBoxSizer(self.images_area,wx.HORIZONTAL)
#self.load_file=wx.Button(self, label='Load File')
#self.sizerBox.Add(self.load_file,0,wx.ALL,5)
self.sizerBox2 = wx.BoxSizer()
self.sizerBox.Add(scroll_img, 1, wx.EXPAND|wx.ALL, 10)
self.sizerBox2.Add(self.sizerBox, 1, wx.EXPAND|wx.ALL, 10)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
# self.sizer.Add(self.button, proportion=0)
btnSizer = wx.BoxSizer() #change to horizontal for side by side
btnTwo = wx.Button(self, label="Load File",size=(200, 40))
btnTwo.Bind(wx.EVT_BUTTON,self.onOpenFile)
bmp1 = None
bitmap1 = None
btnSizer.Add(btnTwo, 0, wx.TOP, 15)
self.sizerBox2.Add(btnSizer)
#self.sizerBox.Add(self.bitmap1)
self.sizer.Add(self.sizerBox2, proportion=1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
areachange(self,self.notebook.GetPageText(0))
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = MyPanel(self)
self.Show()
app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()
Try this to see what the error is,
1.you press create new (any amount of times)
2.press, load file
3. click any item on the listbox (except for the one that you are in)
4. Then go back to the orginal item that you were in,
5. Then click any item, other than the one you are currently in
There will be some sort of problem, the image does not destroy itself and returns an error like the following:
(, PyDeadObjectError('The C++ part of the StaticBitmap object has been deleted, attribute access no longer allowed.',), )
I am still able to load images but the previous images do not delete.
It is hard to word this problem, if anyone can help me with this situation, it would be greatly appreciated. If you need further explanation please comment. I thank you greatly for viewing.
Here you have your code fixed to clear your current image when loading another one.
This is done basically using self.parent.bitmap.Destroy().
I modified some few things without changing the structure of your code, in order for you to recognize changes. I eliminated globals calls. Look how I also eliminated the global IMAGE_LISTSEL variable and converted it in a class attribute. That is what Robin and Fenikso were telling you. Try to do the same with IMAGE_NAME and IMAGE_DATA.
Although the code is working, it is still far from being acceptable wxpython code. You can get many examples of correctly written wxpython code in the web. If you can afford it I recommend to you wxPython in Action from Noel Rappin and Robin Dunn.
IMAGE_NAME = []
IMAGE_DATA = []
import sys
import wx
def deletepic(self):
try:
self.parent.bitmap.Destroy()
except:
print sys.exc_info()
def sendnewpic(self):
if self.parent.bitmap: deletepic(self)
if IMAGE_DATA[self.image_listsel] != '':
try:
print IMAGE_DATA[self.image_listsel]
bmp = wx.Image(IMAGE_DATA[self.image_listsel], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.parent.scroll_img.SetScrollbars(1, 1, bmp.GetWidth(), bmp.GetHeight())
self.parent.bitmap = wx.StaticBitmap(self.parent.scroll_img, -1, bmp, (0, 0))
self.parent.Refresh()
except:
pass
def areachange(self, pg):
print pg
try:
if IMAGE_DATA[self.image_listsel] == '':
deletepic(self)
except:
pass
if pg == "Images":
self.images_area.Show()
else:
self.images_area.Hide()
class imageTab(wx.Panel):
def __init__(self, parent, grandparent):
wx.Panel.__init__(self, parent)
self.parent = grandparent
self.image_listsel = 0
self.listBox = wx.ListBox(self, size=(200, -1), choices=IMAGE_NAME, style=wx.LB_SINGLE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.VERTICAL) #change to horizontal for side by side
self.sizerMain = wx.BoxSizer()
self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
self.listBox.Bind(wx.EVT_LISTBOX, self.imagesel)
btn = wx.Button(self, label="Create New",size=(200, 40))
btnTwo = wx.Button(self, label="Test 2",size=(200, 40))
btn.Bind(wx.EVT_BUTTON, self.newAddImage)
self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
btnSizer.Add(btn, 0, wx.ALL, 5)
btnSizer.Add(btnTwo, 0, wx.ALL, 5)
self.sizer.Add(btnSizer)
self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=0)
self.SetSizer(self.sizerMain)
def imagesel(self, evt):
self.image_listsel = self.listBox.GetSelection()
sendnewpic(self)
def newAddImage(self, evt):
IMAGE_NAME.append('hi')
IMAGE_DATA.append('')
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(len(IMAGE_NAME)-1)
self.imagesel(None) #making it a selected image, globally
def reName(self,parent):
sel = self.listBox.GetSelection()
text = self.listBox.GetString(sel)
renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
if renamed != '':
IMAGE_NAME.pop(sel)
IMAGE_NAME.insert(sel,renamed)
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(sel)
class MyPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.notebook = wx.Notebook(self, size=(225, -1))
#
self.tab_images = imageTab(self.notebook, self)
# add the pages to the notebook with the label to show on the tab
self.notebook.AddPage(self.tab_images, "Pics", select=True)
self.scroll_img = wx.ScrolledWindow(self, -1)
self.scroll_img.SetScrollbars(1, 1, 600, 400)
self.images_area = wx.StaticBox(self, -1, '')
self.sizerBox = wx.StaticBoxSizer(self.images_area, wx.HORIZONTAL)
self.sizerBox2 = wx.BoxSizer()
self.sizerBox.Add(self.scroll_img, 1, wx.EXPAND|wx.ALL, 10)
self.sizerBox2.Add(self.sizerBox, 1, wx.EXPAND|wx.ALL, 10)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
#
btnSizer = wx.BoxSizer() #change to horizontal for side by side
btnTwo = wx.Button(self, label="Load File", size=(200, 40))
btnTwo.Bind(wx.EVT_BUTTON, self.onOpenFile)
self.bmp = None
self.bitmap = None
btnSizer.Add(btnTwo, 0, wx.TOP, 15)
self.sizerBox2.Add(btnSizer)
#
self.sizer.Add(self.sizerBox2, proportion=1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
areachange(self, self.notebook.GetPageText(0))
def OnClickTop(self, event):
self.scroll_img.Scroll(600, 400)
def OnClickBottom(self, event):
self.scroll_img.Scroll(1, 1)
def OnPageChanged(self, event):
new = event.GetSelection()
areachange(self, self.notebook.GetPageText(new))
event.Skip()
def OnPageChanging(self, event):
event.Skip()
def onOpenFile(self, evt):
""" Open a file"""
filename = wx.FileSelector()
if filename != '':
IMAGE_DATA[ self.tab_images.image_listsel] = filename
self.tab_images.imagesel(None)
print IMAGE_DATA
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = MyPanel(self)
self.Show()
app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()
Sometimes you are using bmp1 and bitmap1 as local variables and sometimes as globals. Since you are making multiple instances of them without saving the prior references anywhere then you are losing your references to the already existing objects. When you Destroy() them then you are only destroying the most recently created instances.
Try adding them to some sort of collection (like a list) instead and then you can access any of the items from the list when you need them later. Also try to avoid using global variables. Store your variables in the object instances that they belong to.

wxpython - Creating Unique GUI Elements With a Loop

I am attempting to create a GUI with a large amount of items where there are several sets of the same thing (six labels and six radioboxes).
What I want to do (to save space and for a learning experience) is to create some sort of loop to place these elements on the panel I am using.
Actually placing these should be easy, but the kicker is, I need them to all be unique in some way so I can individually change each label or get each value of each radiobox individually.
Below is the code I have right now, where all the elements are individually created and placed.
sizerMain = wx.BoxSizer()
## For the main control area
panelControl = wx.Panel(self,1,style = wx.MAXIMIZE)
sizerControl = wx.GridBagSizer(hgap = 4,vgap = 4)
# Add widgets
## Main content area
lblTitle = wx.StaticText(panelControl,label = "Pick Scores")
sizerControl.Add(lblTitle,pos = (0,0),
flag = wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM,
border = 5)
self.btnRoll = wx.Button(panelControl,label = "Roll!")
sizerControl.Add(self.btnRoll,pos = (0,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 5)
### Radio boxes
#### Radio button tuple
rboxPick = ["Default","Strength","Dexterity","Constitution",
"Intelligence","Wisdom","Charisma"]
self.lblRoll1 = wx.StaticText(panelControl)
sizerControl.Add(self.lblRoll1,pos = (1,0),flag = wx.ALIGN_CENTER)
self.rboxRoll1 = wx.RadioBox(panelControl,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(panelControl)
sizerControl.Add(self.lblRoll2,pos = (2,0),flag = wx.ALIGN_CENTER)
self.rboxRoll2 = wx.RadioBox(panelControl,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(panelControl)
sizerControl.Add(self.lblRoll3,pos = (3,0),flag = wx.ALIGN_CENTER)
self.rboxRoll3 = wx.RadioBox(panelControl,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(panelControl)
sizerControl.Add(self.lblRoll4,pos = (4,0),flag = wx.ALIGN_CENTER)
self.rboxRoll4 = wx.RadioBox(panelControl,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(panelControl)
sizerControl.Add(self.lblRoll5,pos = (5,0),flag = wx.ALIGN_CENTER)
self.rboxRoll5 = wx.RadioBox(panelControl,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(panelControl)
sizerControl.Add(self.lblRoll6,pos = (6,0),flag = wx.ALIGN_CENTER)
self.rboxRoll6 = wx.RadioBox(panelControl,label = "Roll Six",choices = rboxPick)
sizerControl.Add(self.rboxRoll6,pos = (6,1),span = (1,5),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
Also, it is late.. So if I am not making sense please let me know and I will be happy to re-explain..
Its been a while since I've done any wxPython coding so I'm a little rusty, but off the top of my head I can think of two ways of doing this other than neurino's solution, although there are other variations.
Method 1
Keep references to each widget in a dictionary, using the widgets label as the key, eg
rboxPick = ["Default", "Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom", "Charisma"]
labels = ["One", "Two", "Three", "Four"]
self.rollRbs = dict()
#create the radioBoxes..
for row, label in enumerate(labels):
lbl = wx.StaticText(panelControl)
rbox = wx.RadioBox(panelControl, label="Roll %s"%(label),
choices=rboxPick)
sizerControl.Add(rbox ,pos = (row, 1),span=(1,5),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border = 2)
self.rollRbs[rbox.GetLabel()] = rbox
#changing the label...
self.rollRbs["Roll One"].SetLabel("blah")
Method 2
Personally I prefer a more event driven approach. Simply bind each RadioBoxes event to the same handler. Then in the handler you can differentiate between the RadioBoxes using their label attributes.
Working example:
import wx
class GUI(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(700, 400))
panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE)
sizerControl = wx.GridBagSizer(hgap=4,vgap = 4)
lblTitle = wx.StaticText(panelControl, label="Pick Scores")
self.btnRoll = wx.Button(panelControl, label="Roll!")
sizerControl.Add(lblTitle, pos=(0,0),
flag=wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
sizerControl.Add(self.btnRoll, pos=(0,1),
span=(1,5), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
rboxPick = ["Default", "Strength", "Dexterity", "Constitution",
"Intelligence", "Wisdom", "Charisma"
]
labels = ["One", "Two", "Three", "Four"]
#Create, layout and bind the RadioBoxes
for row, label in enumerate(labels):
lbl = wx.StaticText(panelControl)
rbox = wx.RadioBox(panelControl, label="Roll %s"%(label), choices=rboxPick)
self.Bind(wx.EVT_RADIOBOX, self.onRadioBox, rbox)
sizerControl.Add(rbox, pos=(row+1, 1), span=(1,5),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=2)
sizerMain = wx.BoxSizer()
sizerMain.Add(sizerControl)
panelControl.SetSizerAndFit(sizerMain)
def onRadioBox(self, evt):
"""Event handler for RadioBox.."""
rbox = evt.GetEventObject()#Get a reference to the RadioBox
rboxLbl = rbox.GetLabel() #We can identify the RadioBox with its label
selection = rbox.GetSelection()
print rboxLbl
print selection
if rboxLbl == "Roll One":
#do something
pass
elif rboxLbl == "Roll Two":
#do something else
pass
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = GUI(None, -1, "")
frame.Show(1)
app.MainLoop()
If for some reason you need that empty StaticText that you have paired with each RadioBox, then I would probably just make that pair a rich compostite widget, with some methods for changing the label etc. Then use method 2 to create and update them. If you need to modify these widgets attributes outside of the event handler after they are created then I think your going need to keep references to them in some shape or form e.g method 1.
Here is a working example
import wx
import wx.lib.newevent
class LblRadBox(wx.Panel):
"""
Simple example of a composite widget
Add methods as required to improve functionality...
"""
def __init__(self, parent, stLbl="", rbLbl="", choices=[]):
wx.Panel.__init__(self, parent)
self.stLbl = wx.StaticText(self, label=stLbl)
self.rbox = wx.RadioBox(self, label=rbLbl, choices=choices)
sizer = wx.BoxSizer()
sizer.Add(self.stLbl)
sizer.Add(self.rbox)
self.SetSizerAndFit(sizer)
def SetSTLabel(self, lbl):
self.stLbl.SetLabel(lbl)
def GetLabel(self):
return self.rbox.GetLabel()
def GetSelection(self, lbl):
return self.rbox.GetSelection()
class GUI(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(700, 400))
panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE)
sizerControl = wx.GridBagSizer(hgap=4,vgap = 4)
lblTitle = wx.StaticText(panelControl, label="Pick Scores")
self.btnRoll = wx.Button(panelControl, label="Roll!")
sizerControl.Add(lblTitle, pos=(0,0),
flag=wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
sizerControl.Add(self.btnRoll, pos=(0,1),
span=(1,5), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
rboxPick = ["Default", "Strength", "Dexterity", "Constitution",
"Intelligence", "Wisdom", "Charisma"
]
labels = ["One", "Two", "Three", "Four"]
#Create, layout and bind the RadioBoxes
for row, label in enumerate(labels):
rbox = LblRadBox(panelControl, rbLbl="Roll %s"%(label), choices=rboxPick)
#if u want to be able to access the rboxes outside of onRadioBox()
#then add references of them to a dictionary like in method 1..
sizerControl.Add(rbox, pos=(row+1, 1), span=(1,5),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=2)
panelControl.Bind(wx.EVT_RADIOBOX, self.onRadioBox)
sizerMain = wx.BoxSizer()
sizerMain.Add(sizerControl)
panelControl.SetSizerAndFit(sizerMain)
def onRadioBox(self, evt):
"""Event handler for RadioBox.."""
rbox = evt.GetEventObject()#Get a reference to the RadioBox
rboxLbl = rbox.GetLabel() #We can identify the RadioBox with its label
selection = rbox.GetSelection()
print rboxLbl
print selection
if rboxLbl == "Roll One":
#do something
pass
elif rboxLbl == "Roll Two":
#do something else
pass
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = GUI(None, -1, "")
frame.Show(1)
app.MainLoop()
They will be unique anyway, if you do not provide an id you can create with wx.NewId() wx will create one for you.
If you take care of creating (or retrieving) ids, and store them (in a list, in a dict, you choose), then you'll be able to get back to all single element to edit it.
ids = []
for lbl in ('Name', 'Surname', 'Address'):
st = wx.StaticText(panel)
tc = wx.TextControl(panel, label=lbl)
ids.append(tc.GetId())
Anyway you should be able to make your edits, that usually respond to user actions, only using events data without the need of storing any id.

Categories