Learning Python (coming into it as a strong C# programmer), excited about the power of SciPy and Matplotlib, so I am looking to add a matplotlib figure to a panel. The panel is in a notebook, whose code is generated as the output from wxFormBuilder (see "Main_gui.py" file).
The issue I have is that I am getting an error, and it persists after trying many perturbations of the code:
AttributeError: type object 'MainForm' has no attribute 'page_Data'
Here's my files:
"Main.py":
import wx
import Main_gui
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
class MainApp( Main_gui.MainForm ):
# === INITIALIZATION =================================================================================
def __init__(self, parent):
#Initialize the main form
Main_gui.MainForm.__init__( self, parent )
#Create chart
self.sizer = self.page_Data.GetSizer()
self.plot_container = self.page_Data
self.plotpanel = CreatePlot(self.plot_container)
self.sizer.Add(self.plotpanel, 1, wx.EXPAND)
self.plot_container.SetSizer(self.sizer)
class CreatePlot(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
self.figure = Figure(figsize=(5,4), dpi=100)
self.axes = self.figure.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
self.axes.plot(t,s)
self.canvas = FigureCanvas(self, -1, self.figure)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
Main_gui.MainForm.page_Data.SetSizer(sizer)
Main_gui.MainForm.page_Data.Fit()
if __name__ == '__main__':
# === RUN ==========================================================================================
#Get the wxApp
app = wx.App(0)
#Show the form
MainApp(None).Show()
#Run
app.MainLoop()
"Main_gui.py":
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Oct 8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class MainForm
###########################################################################
class MainForm ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"MainForm", pos = wx.DefaultPosition, size = wx.Size( 813,512 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
sizer_Main = wx.BoxSizer( wx.VERTICAL )
self.m_notebook2 = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.NB_BOTTOM|wx.NB_FIXEDWIDTH )
self.page_Connections = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
flexgrid_Connections = wx.FlexGridSizer( 2, 0, 10, 0 )
flexgrid_Connections.AddGrowableCol( 0 )
flexgrid_Connections.AddGrowableRow( 1 )
flexgrid_Connections.SetFlexibleDirection( wx.BOTH )
flexgrid_Connections.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
sizer1_Connections = wx.StaticBoxSizer( wx.StaticBox( self.page_Connections, wx.ID_ANY, u"Connection" ), wx.VERTICAL )
sizer1grid_Connections = wx.GridSizer( 0, 3, 0, 0 )
self.m_staticText3 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"IP Address:", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_LEFT )
self.m_staticText3.Wrap( -1 )
sizer1grid_Connections.Add( self.m_staticText3, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 5 )
self.txt_Connections_IPaddress = wx.TextCtrl( self.page_Connections, wx.ID_ANY, u"10.0.0.3", wx.DefaultPosition, wx.DefaultSize, wx.TE_LEFT )
self.txt_Connections_IPaddress.SetMaxLength( 15 )
sizer1grid_Connections.Add( self.txt_Connections_IPaddress, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.btn_Connections_Connect = wx.Button( self.page_Connections, wx.ID_ANY, u"Connect", wx.DefaultPosition, wx.DefaultSize, 0 )
sizer1grid_Connections.Add( self.btn_Connections_Connect, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
sizer1_Connections.Add( sizer1grid_Connections, 0, 0, 5 )
flexgrid_Connections.Add( sizer1_Connections, 0, wx.EXPAND, 5 )
sizer2_Connections = wx.StaticBoxSizer( wx.StaticBox( self.page_Connections, wx.ID_ANY, u"Status" ), wx.VERTICAL )
fgSizer5 = wx.FlexGridSizer( 10, 4, 0, 0 )
fgSizer5.AddGrowableCol( 2 )
fgSizer5.SetFlexibleDirection( wx.BOTH )
fgSizer5.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.m_staticText5 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Device Name:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText5.Wrap( -1 )
fgSizer5.Add( self.m_staticText5, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 5 )
self.txt_Connections_DeviceName = wx.TextCtrl( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
fgSizer5.Add( self.txt_Connections_DeviceName, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.btn_Connections_RenameDevice = wx.Button( self.page_Connections, wx.ID_ANY, u"Rename", wx.DefaultPosition, wx.DefaultSize, wx.BU_EXACTFIT )
fgSizer5.Add( self.btn_Connections_RenameDevice, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText6 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"ADC Channels:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText6.Wrap( -1 )
fgSizer5.Add( self.m_staticText6, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 5 )
self.txt_Connections_ADCchannels = wx.TextCtrl( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
fgSizer5.Add( self.txt_Connections_ADCchannels, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.btn_Connections_SetADC = wx.Button( self.page_Connections, wx.ID_ANY, u"Set", wx.DefaultPosition, wx.DefaultSize, wx.BU_EXACTFIT )
fgSizer5.Add( self.btn_Connections_SetADC, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText15 = wx.StaticText( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText15.Wrap( -1 )
fgSizer5.Add( self.m_staticText15, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText16 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"HVPS:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText16.Wrap( -1 )
fgSizer5.Add( self.m_staticText16, 0, wx.ALL, 5 )
self.m_staticText17 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText17.Wrap( -1 )
fgSizer5.Add( self.m_staticText17, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText18 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Setpoint:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText18.Wrap( -1 )
fgSizer5.Add( self.m_staticText18, 0, wx.ALL, 5 )
self.m_staticText19 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"0.00 V", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText19.Wrap( -1 )
fgSizer5.Add( self.m_staticText19, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText20 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Stabilized Probe:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText20.Wrap( -1 )
fgSizer5.Add( self.m_staticText20, 0, wx.ALL, 5 )
self.m_staticText21 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"False", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText21.Wrap( -1 )
fgSizer5.Add( self.m_staticText21, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText22 = wx.StaticText( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText22.Wrap( -1 )
fgSizer5.Add( self.m_staticText22, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText7 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Serial Number:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText7.Wrap( -1 )
fgSizer5.Add( self.m_staticText7, 0, wx.ALL, 5 )
self.m_staticText8 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"000000", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText8.Wrap( -1 )
fgSizer5.Add( self.m_staticText8, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText9 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Telnet:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText9.Wrap( -1 )
fgSizer5.Add( self.m_staticText9, 0, wx.ALL, 5 )
self.m_staticText10 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText10.Wrap( -1 )
fgSizer5.Add( self.m_staticText10, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_staticText11 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"TFTP:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText11.Wrap( -1 )
fgSizer5.Add( self.m_staticText11, 0, wx.ALL, 5 )
self.m_staticText12 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText12.Wrap( -1 )
fgSizer5.Add( self.m_staticText12, 0, wx.ALL, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
sizer2_Connections.Add( fgSizer5, 1, wx.EXPAND, 5 )
flexgrid_Connections.Add( sizer2_Connections, 0, wx.EXPAND, 5 )
self.page_Connections.SetSizer( flexgrid_Connections )
self.page_Connections.Layout()
flexgrid_Connections.Fit( self.page_Connections )
self.m_notebook2.AddPage( self.page_Connections, u"Connections", False )
self.page_Acquisition = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Acquisition, u"Acquisition", False )
self.page_Data = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer3 = wx.BoxSizer( wx.VERTICAL )
self.page_Data.SetSizer( bSizer3 )
self.page_Data.Layout()
bSizer3.Fit( self.page_Data )
self.m_notebook2.AddPage( self.page_Data, u"Data", True )
self.page_Calibration = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Calibration, u"Calibration", False )
self.page_Analyses = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Analyses, u"Analyses", False )
self.page_Locator = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Locator, u"Locator", False )
self.page_Hardware = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Hardware, u"Hardware", False )
self.page_Log = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_notebook2.AddPage( self.page_Log, u"Log", False )
sizer_Main.Add( self.m_notebook2, 1, wx.BOTTOM|wx.EXPAND|wx.TOP, 5 )
self.SetSizer( sizer_Main )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
Traceback:
Traceback (most recent call last):
File "<project location>\Main.py", line 56, in <module>
MainApp(None).Show()
File "<project location>\Main.py", line 27, in __init__
self.plotpanel = CreatePlot(self.plot_container)
File "<project location>\Main.py", line 47, in __init__
Main_gui.MainForm.page_Data.SetSizer(sizer)
AttributeError: type object 'MainForm' has no attribute 'page_Data'
19:12:12: Debug: ..\..\include\wx/msw/private.h(697): 'UnregisterClass' failed with error 0x00000584 (class still has open windows.).
So, what am I doing wrong? Thanks!
Recent edit, thanks to Yoriz:
class MainApp( Main_gui.MainForm ):
# === INITIALIZATION =================================================================================
def __init__(self, parent):
#Initialize the main form
Main_gui.MainForm.__init__( self, parent )
#Create chart
self.sizer = self.page_Data.GetSizer()
self.plot_container = self.page_Data
self.plotpanel = CreatePlot(self.plot_container)
class CreatePlot(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
self.figure = Figure(figsize=(5,4), dpi=100)
self.axes = self.figure.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
self.axes.plot(t,s)
self.canvas = FigureCanvas(parent, -1, self.figure)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
parent.SetSizer(sizer)
parent.Fit()
It looks like in class CreatePlot you are doing calls class calls at the end of it
Main_gui.MainForm.page_Data.SetSizer(sizer)
Main_gui.MainForm.page_Data.Fit()
These need to be instance calls to access those methods
I think you might want them to be
parent.page_Data.SetSizer(sizer)
parent.page_Data.Fit()
Related
I'm trying to build an application in Python using wxpython library.
I would like to add and remove widgets dinamically like in the picture
(https://i.stack.imgur.com/bNB3q.png) so that i can add and remove how many StaticBoxSizer I want.
I got a good result in adding widgets while I can't get the way to remove the widgets. I'm successfull in destroying every widget in the StaticBoxSizer but I'm not able to destroy the container StaticBoxSizer itself: if I try to add many blocks and then removing one of them, in the onRemoveMaster method something happens and Python crash.
My code:
import wx
import wx.xrc
import wx.aui
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size = wx.Size( 726,624 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.number_of_added = 0
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
#Setup MenuBar
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menubar1.Append( self.m_menu1, u"MyMenu" )
self.m_menu2 = wx.Menu()
self.m_menubar1.Append( self.m_menu2, u"MyMenu" )
self.SetMenuBar( self.m_menubar1 )
#Setup Main Container
MainSizer = wx.BoxSizer( wx.VERTICAL )
self.ScrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.ScrolledWindow.SetScrollRate( 1, 1 )
MainVerticalSizer = wx.BoxSizer( wx.VERTICAL )
self.m_auinotebook1 = wx.aui.AuiNotebook( self.ScrolledWindow, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.aui.AUI_NB_DEFAULT_STYLE )
#Costruzione Master Sequence Tab
self.MasterSequenceTab = wx.ScrolledWindow( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.MasterSequenceTab.SetScrollRate( 1, 1 )
self.MasterVSizer = wx.BoxSizer( wx.VERTICAL )
MasterButtonSizer = wx.BoxSizer( wx.HORIZONTAL )
self.ButtonAdd = wx.Button( self.MasterSequenceTab, wx.ID_ANY, u"Add", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonAdd, 0, wx.ALL, 5 )
self.ButtonAdd.Bind(wx.EVT_BUTTON, self.onAddMaster)
self.ButtonRemove = wx.Button( self.MasterSequenceTab, wx.ID_ANY, u"Remove", wx.DefaultPosition, wx.DefaultSize, 0 )
MasterButtonSizer.Add( self.ButtonRemove, 0, wx.ALL, 5 )
self.ButtonRemove.Bind(wx.EVT_BUTTON, self.onRemoveMaster)
self.MasterVSizer.Add( MasterButtonSizer, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )
self.onAddMaster(self)
self.m_auinotebook1.AddPage( self.MasterSequenceTab, u"Master Sequence", False )
#Finalizzazione finestre
MainVerticalSizer.Add( self.m_auinotebook1, 1, wx.EXPAND, 5 )
self.ScrolledWindow.SetSizer( MainVerticalSizer )
self.ScrolledWindow.Layout()
MainVerticalSizer.Fit( self.ScrolledWindow )
MainSizer.Add( self.ScrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( MainSizer )
self.Layout()
self.Centre( wx.BOTH )
def onAddMaster(self, event):
self.number_of_added += 1
self.MastersbSizerAdded = wx.StaticBoxSizer( wx.StaticBox( self.MasterSequenceTab, 100+self.number_of_added, wx.EmptyString ),wx.VERTICAL )
self.MasterCheckEnableAdded = wx.CheckBox( self.MastersbSizerAdded.GetStaticBox(), 200+self.number_of_added, u"Enable", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterCheckEnableAdded.SetValue(True)
self.MastersbSizerAdded.Add( self.MasterCheckEnableAdded, 0, wx.ALIGN_RIGHT|wx.ALL, 5 )
MasterfgSizerAdded = wx.FlexGridSizer( 0, 2, 0, 0 )
MasterfgSizerAdded.SetFlexibleDirection( wx.BOTH )
MasterfgSizerAdded.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.MasterTextNomeAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1000+self.number_of_added, u"Nome", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextNomeAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextNomeAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlNomeAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 300+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlNomeAdded, 0, wx.ALL, 5 )
self.MasterTextDescrizioneAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1100+self.number_of_added, u"Descrizione ", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextDescrizioneAdded.Wrap( -1 )
MasterfgSizerAdded.Add( self.MasterTextDescrizioneAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlDescrizione1 = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 400+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.Size( 566,-1 ), 0 )
MasterfgSizerAdded.Add( self.MasterCtrlDescrizione1, 0, wx.ALL, 5 )
self.MastersbSizerAdded.Add( MasterfgSizerAdded, 0, wx.ALL, 5 )
MasterfgSizerAdded2 = wx.FlexGridSizer( 3, 6, 0, 0 )
MasterfgSizerAdded2.SetFlexibleDirection( wx.BOTH )
MasterfgSizerAdded2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.MasterTextSubjectAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1200+self.number_of_added, u"Subject Area", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextSubjectAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextSubjectAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlSubjectAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 500+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlSubjectAdded, 0, wx.ALL, 5 )
self.MasterTextAlimentanteAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1300+self.number_of_added, u" Alimentante", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextAlimentanteAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextAlimentanteAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlAlimentanteAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 600+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlAlimentanteAdded, 0, wx.ALL, 5 )
self.MasterTextGroupAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1400+self.number_of_added, u" Num group", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextGroupAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextGroupAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.MasterCtrlGroupAdded = wx.TextCtrl( self.MastersbSizerAdded.GetStaticBox(), 700+self.number_of_added, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlGroupAdded, 0, wx.ALL, 5 )
self.MasterTextSchedulingAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1500+self.number_of_added, u"Schedulazione", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextSchedulingAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextSchedulingAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
MasterCtrlSchedulingChoicesAdded = [ u"G", u"S", u"D", u"Q", u"M", u"BM", u"TM", u"SM", u"A" ]
self.MasterCtrlSchedulingAdded = wx.Choice( self.MastersbSizerAdded.GetStaticBox(), 800+self.number_of_added, wx.DefaultPosition, wx.DefaultSize, MasterCtrlSchedulingChoicesAdded, 0 )
self.MasterCtrlSchedulingAdded.SetSelection( 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlSchedulingAdded, 0, wx.ALL, 5 )
self.MasterTextValidoAdded = wx.StaticText( self.MastersbSizerAdded.GetStaticBox(), 1600+self.number_of_added, u" Record valido", wx.DefaultPosition, wx.DefaultSize, 0 )
self.MasterTextValidoAdded.Wrap( -1 )
MasterfgSizerAdded2.Add( self.MasterTextValidoAdded, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
MasterCtrlValido1Choices = [ u"Y", u"N" ]
self.MasterCtrlValidoAdded = wx.Choice( self.MastersbSizerAdded.GetStaticBox(), 900+self.number_of_added, wx.DefaultPosition, wx.DefaultSize, MasterCtrlValido1Choices, 0 )
self.MasterCtrlValidoAdded.SetSelection( 0 )
MasterfgSizerAdded2.Add( self.MasterCtrlValidoAdded, 0, wx.ALL, 5 )
self.MastersbSizerAdded.Add( MasterfgSizerAdded2, 0, wx.ALL, 5 )
self.MasterVSizer.Add( self.MastersbSizerAdded, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.MasterSequenceTab.SetSizer( self.MasterVSizer )
self.MasterSequenceTab.Layout()
self.MasterVSizer.Fit( self.MasterSequenceTab )
def onRemoveMaster(self, event):
for i in range(16,0,-1):
button = 100*i+self.number_of_added
widget = self.MasterSequenceTab.FindWindowById(button)
widget.Destroy()
self.MasterSequenceTab.SetSizer( self.MasterVSizer )
self.MasterSequenceTab.Layout()
self.MasterVSizer.Fit( self.MasterSequenceTab )
self.number_of_added -= 1
if __name__ == '__main__':
app = wx.App()
Example(None, title='Test')
app.MainLoop()
It would be much easier to create a new panel class with all those controls parented to it, and then just add and delete instances of the panel.
The panel instances could be stored in a list.
I have built a GUI using wxFormBuilder and want to dynamically add buttons to a sizer within the GUI. Here is the wxFormBuilder code.
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class HighVoltage
###########################################################################
class HighVoltage ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 1031,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )
bSizer84 = wx.BoxSizer( wx.VERTICAL )
self.m_panel70 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer85 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText79 = wx.StaticText( self.m_panel70, wx.ID_ANY, u"HV Documentation", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText79.Wrap( -1 )
self.m_staticText79.SetFont( wx.Font( 20, 70, 90, 92, False, "Century Gothic" ) )
bSizer85.Add( self.m_staticText79, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
bSizer85.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_bitmap24 = wx.StaticBitmap( self.m_panel70, wx.ID_ANY, wx.Bitmap( u"LOGOtransSmall.png", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer85.Add( self.m_bitmap24, 0, wx.ALL, 5 )
self.m_panel70.SetSizer( bSizer85 )
self.m_panel70.Layout()
bSizer85.Fit( self.m_panel70 )
bSizer84.Add( self.m_panel70, 0, wx.EXPAND |wx.ALL, 5 )
self.m_panel71 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer86 = wx.BoxSizer( wx.VERTICAL )
bSizer86.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_panel73 = wx.Panel( self.m_panel71, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer86.Add( self.m_panel73, 0, wx.EXPAND |wx.ALL, 5 )
self.m_panel71.SetSizer( bSizer86 )
self.m_panel71.Layout()
bSizer86.Fit( self.m_panel71 )
bSizer84.Add( self.m_panel71, 1, wx.EXPAND |wx.ALL, 5 )
self.m_panel72 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer87 = wx.BoxSizer( wx.HORIZONTAL )
bSizer87.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.m_button65 = wx.Button( self.m_panel72, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer87.Add( self.m_button65, 0, wx.ALL, 5 )
self.m_panel72.SetSizer( bSizer87 )
self.m_panel72.Layout()
bSizer87.Fit( self.m_panel72 )
bSizer84.Add( self.m_panel72, 0, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( bSizer84 )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
I want to add buttons to bSizer86 which is a child of m_panel71.
Here's what I've tried so far.
class HvDocs(HighVoltage):
def __init__(self, parent):
HighVoltage.__init__(self, parent)
con = sqlite3.connect('hsmanagement.sqlite')
cur = con.execute('SELECT employee FROM employees WHERE hv=1 ORDER BY employee')
result = cur.fetchall()
con.close()
print result
sizer = self.GetSizer()
print type(sizer)
for f in range(0, len(result) ):
sizer.Add(wx.Button(self, label=str(result[f][0]), id=int(f), size=(200, 40)), 2, wx.CENTER)
I have tried a few variations of sizer = self.GetSizer() such as sizer = self.m_panel71 and sizer = self.GetSizer(self.m_panel71) but all I can manage is to add the buttons to the top level sizer bSizer84. How do I gain access to bSizer86?
See the following minimal example how to find panels, buttons and sizers by name only. wxFormBuilder offers the possibility to set the wx.Window property window_name for GUI elements you want to keep track on. You should be able to find control you need to know having this name with parent.FindWindowByName(…) without relying on a class attribute.
import wx
TOK_PNL = 'window_name pnl'
TOK_BTN = 'window_name btn'
def on_btn(evt, parent):
btn = parent.FindWindowByName(TOK_BTN)
pnl = parent.FindWindowByName(TOK_PNL)
newbtn = wx.Button(pnl, -1, 'newbtn')
szsub = btn.GetContainingSizer()
szsub.Add(newbtn, 1, wx.EXPAND|wx.ALL, 4)
pnl.Layout()
class testfrm(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
pnl = wx.Panel(self, -1)
pnl.SetName(TOK_PNL)
btn = wx.Button(pnl, -1, 'Add Button')
btn.SetName(TOK_BTN)
szmain = wx.BoxSizer(wx.VERTICAL)
szsub = wx.BoxSizer(wx.HORIZONTAL)
szsub.Add(btn, 0, wx.EXPAND|wx.ALL, 4)
szmain.Add(szsub)
pnl.SetSizer(szmain)
the_parent = self
btn.Bind(wx.EVT_BUTTON, lambda evt: on_btn(evt, the_parent))
if __name__ == '__main__':
app = wx.App(redirect=False)
frm = testfrm(None, -1, 'testfrm')
frm.Show()
app.MainLoop()
After digging a bit further and playing around I finally solved my problem with the following code
sizer = self.m_button67.GetContainingSizer()
self.m_button67.Destroy()
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= "Ladder ID")
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= "Engineer")
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= "Description")
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= "Last Check")
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= "Next Check")
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
for f in range(0, len(ladder_data)):
self.m_button = wx.Button( self.m_panel74, id=int(f), label=str(ladder_data[f][0]))
self.Bind(wx.EVT_BUTTON, self.detect_on_button)
sizer.Add( self.m_button, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= str(ladder_data[f][1]))
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= str(ladder_data[f][2]))
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= str(ladder_data[f][3]))
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= str(ladder_data[f][4]))
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_panel74.SetSizer( sizer )
self.m_panel74.Layout()
sizer.Fit( self.m_panel74 )
The key really was the discovery of GetContainingSizer().
Using this with a dummy button set up in wxFormBuilder (self.m_button67)
sizer = self.m_button67.GetContainingSizer()
gets you the sizer
remove the dummy button with
self.m_button67.Destroy()
add you widgets with
self.m_staticText = wx.StaticText( self.m_panel74, wx.ID_ANY, label= str(ladder_data[f][1]))
sizer.Add( self.m_staticText, 0, wx.ALL, 5 )
and the layout is completed with
self.m_panel74.SetSizer( sizer )
self.m_panel74.Layout()
sizer.Fit( self.m_panel74 )
Hope this help anyone with the same problem.
Maybe a little late,
I believe you are using GetSizer on Wrong element.
Try switching sizer = self.GetSizer() by sizer = self.m_panel71.GetSizer()
I am creating multi-column lists which are all equal in length and also generating number of radio buttons equal to the length of list. I have couple of issues:
1] Display issue: In following fig., I get radio buttons.
But when I scroll down this is what happens.
Following is a snippet of my code to generate it Please assist me to fix this issue so that I get full list of radio buttons displayed properly.
w = 0
for i in range(1,len(lut_code)):
w += 30
rb_G = wx.RadioButton(scroll1, -1, "G", (500,w), style=wx.RB_GROUP)
rb_F = wx.RadioButton(scroll1, -1, "F", (540,w))
rb_P = wx.RadioButton(scroll1, -1, "P", (580,w))
2] Selection of radio button: When I want to select a single radio button from a row, a complete row is selected instead and it gets blue in color like in following fig.
Is it because of my use of wx.ListCtrl to display these columns? What would be the fix or alternative method to only select radio button of choice instead of selecting whole row?
Took me some time to figure this out. I think you are actually drawing the radio buttons on the frame by specifying the position! This will obviously not work in this case because your listCtrl comes to the foreground when you try to select a radio button.
If you notice the radio buttons are not correctly aligned according to the list items. The gap between the buttons is greater than the gap between rows of the list.
The problem with using list_ctrl is you cannot put widgets like radio buttons in them.
I would suggest you to use something like wx.ScrolledWindow for this. Put each row into a sizer and stack these sizers vertically.
UPDATE:
import wx
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
sizer0 = wx.BoxSizer( wx.VERTICAL )
self.scrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
self.scrolledWindow.SetScrollRate( 5, 5 )
grid_sizer = wx.GridSizer( 0, 8, 0, 0 )
self.head1 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Code", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head1.Wrap( -1 )
grid_sizer.Add( self.head1, 0, wx.ALL, 5 )
self.head2 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Classification", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head2.Wrap( -1 )
grid_sizer.Add( self.head2, 0, wx.ALL, 5 )
self.head3 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"A", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head3.Wrap( -1 )
grid_sizer.Add( self.head3, 0, wx.ALL, 5 )
self.head4 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"B", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head4.Wrap( -1 )
grid_sizer.Add( self.head4, 0, wx.ALL, 5 )
self.head5 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"C", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head5.Wrap( -1 )
grid_sizer.Add( self.head5, 0, wx.ALL, 5 )
self.head6 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"D", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head6.Wrap( -1 )
grid_sizer.Add( self.head6, 0, wx.ALL, 5 )
self.head7 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Cond.", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head7.Wrap( -1 )
grid_sizer.Add( self.head7, 0, wx.ALL, 5 )
self.head8 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Update", wx.DefaultPosition, wx.DefaultSize, 0 )
self.head8.Wrap( -1 )
grid_sizer.Add( self.head8, 0, wx.ALL, 5 )
self.column11 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column11.Wrap( -1 )
grid_sizer.Add( self.column11, 0, wx.ALL, 5 )
self.column12 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Urban", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column12.Wrap( -1 )
grid_sizer.Add( self.column12, 0, wx.ALL, 5 )
self.column13 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"68", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column13.Wrap( -1 )
grid_sizer.Add( self.column13, 0, wx.ALL, 5 )
self.column14 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"80", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column14.Wrap( -1 )
grid_sizer.Add( self.column14, 0, wx.ALL, 5 )
self.column15 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column15.Wrap( -1 )
grid_sizer.Add( self.column15, 0, wx.ALL, 5 )
self.column16 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"89", wx.DefaultPosition, wx.DefaultSize, 0 )
self.column16.Wrap( -1 )
grid_sizer.Add( self.column16, 0, wx.ALL, 5 )
self.column17 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.column17.Wrap( -1 )
grid_sizer.Add( self.column17, 0, wx.ALL, 5 )
radio1Choices = [ u"G", u"P", u"F" ]
self.radio1 = wx.RadioBox( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, radio1Choices, 3, wx.RA_SPECIFY_COLS )
self.radio1.SetSelection( 0 )
grid_sizer.Add( self.radio1, 0, wx.ALL, 5 )
self.scrolledWindow.SetSizer( grid_sizer )
self.scrolledWindow.Layout()
grid_sizer.Fit( self.scrolledWindow )
sizer0.Add( self.scrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )
# Bind the radio box select event to a function
self.radio1.Bind( wx.EVT_RADIOBOX, self.on_selected )
self.SetSizer( sizer0 )
self.Layout()
self.Maximize()
self.Centre( wx.BOTH )
self.Show()
def on_selected(self, event):
# Depending upon the option selected the values of A,B,C,D are changed
if self.radio1.GetStringSelection() == 'P':
self.column13.SetLabel('25')
self.column14.SetLabel('27')
self.column15.SetLabel('34')
self.column16.SetLabel('12')
elif self.radio1.GetStringSelection() == 'F':
self.column13.SetLabel('56')
self.column14.SetLabel('70')
self.column15.SetLabel('49')
self.column16.SetLabel('54')
else:
self.column13.SetLabel('78')
self.column14.SetLabel('83')
self.column15.SetLabel('69')
self.column16.SetLabel('100')
if __name__ == "__main__":
app = wx.App()
Frame(None)
app.MainLoop()
I wrote this sample code to show you how you can do it. It's a bit crude but I hope you get the picture.
You might also want to check the answer in this post:Adding a button to every row in ListCtrl WxPython. It might be a better choice.
Am working with multiple wx.Choice control in wxPython and I needed the make a reset button to restore the default option “-- Select --” when clicked.
Am unable to achieve that, the closest I came is to reset to empty options which is NOT what is want. I want the default option “-- Select --” to come up when reset button is pressed. See my code below.
data.py
import wx
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 250,300 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
DataBox = wx.BoxSizer( wx.HORIZONTAL )
gSizer2 = wx.GridSizer( 0, 2, 0, 0 )
self.Color_Label = wx.StaticText( self, wx.ID_ANY, u"Color:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Color_Label.Wrap( -1 )
self.Color_Label.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Color_Label, 0, wx.ALL, 5 )
Color_optionsChoices = [ u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown" ]
self.Color_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Color_optionsChoices, 0 )
self.Color_options.SetSelection( 0 )
gSizer2.Add( self.Color_options, 0, wx.ALL, 5 )
self.Age = wx.StaticText( self, wx.ID_ANY, u"Age:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Age.Wrap( -1 )
self.Age.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Age, 0, wx.ALL, 5 )
Age_optionsChoices = [ u"-- Select --", u"15", u"16", u"17", u"18", u"19", u"20", u"21", u"22", u"23", u"24", u"25", wx.EmptyString ]
self.Age_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Age_optionsChoices, 0 )
self.Age_options.SetSelection( 0 )
gSizer2.Add( self.Age_options, 0, wx.ALL, 5 )
self.Country = wx.StaticText( self, wx.ID_ANY, u"Country:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Country.Wrap( -1 )
self.Country.SetFont( wx.Font( 13, 70, 90, 90, False, wx.EmptyString ) )
gSizer2.Add( self.Country, 0, wx.ALL, 5 )
Country_optionsChoices = [ u"-- Select --", u"Mexico", u"Peru", u"India", u"USA", u"UK", u"Greece", u"Agentina", u"Greece", u"Brazil", u"Egypt" ]
self.Country_options = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Country_optionsChoices, 0 )
self.Country_options.SetSelection( 0 )
gSizer2.Add( self.Country_options, 0, wx.ALL, 5 )
self.Reset_button = wx.Button( self, wx.ID_ANY, u"Reset", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.Reset_button, 0, wx.ALL, 5 )
self.Exit_button = wx.Button( self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.Exit_button, 0, wx.ALL, 5 )
DataBox.Add( gSizer2, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )
self.SetSizer( DataBox )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )
self.Exit_button.Bind( wx.EVT_BUTTON, self.OnExitButton )
def OnResetButton( self, event ):
# val = '-- Select --' # NOT WORKING
val = ' ' # THIS WORKS, BUT RESETS TO EMPTY CHOICE
self.Color_options.SetLabel(val)
self.Age_options.SetLabel(val)
self.Country_options.SetLabel(val)
def OnExitButton( self, event ):
self.Close()
app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()
Thanks for your time in advance.
Please reduce your source code to a minimal runnable example (explained below).
Answer: I am amazed that wx.Choice.SetLabel(' ') does something useful. What you want to do instead is:
self.Color_Options.SetStringSelection(val)
(see the wxWidgets documentation for wxChoice/wxItemContainer).
Remark: u'--Select--' is not '--Select--' in Python. As it happens, if the source encoding is set to UTF-8, wxPython will not complain and understand str encoden in UTF-8 as well as u''.
Minimal runnable example: As important as ever: Making your example as small as possible will teach you which parts are relevant for your question and which are not. In many cases, by writing the minimal example, I very often find the answer myself.
Minimising your example:
import wx
class MyFrame1(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
pnl = wx.Panel(self, wx.ID_ANY)
szmain = wx.BoxSizer(wx.VERTICAL)
color_choices = [u"-- Select --", u"Red", u"Green", u"Pink", u"Blue", u"Yellow", u"White", u"Brown"]
self.color_options = wx.Choice(pnl, wx.ID_ANY, choices=color_choices)
self.color_options.SetSelection(0)
self.reset_button = wx.Button(pnl, wx.ID_ANY, u"Reset")
szmain.Add(self.color_options, 0, wx.ALL|wx.EXPAND, 4)
szmain.Add(self.reset_button, 0, wx.ALL|wx.EXPAND, 4)
pnl.SetSizer(szmain)
self.reset_button.Bind( wx.EVT_BUTTON, self.OnResetButton )
def OnResetButton(self, event):
val = '-- Select --' # NOT WORKING
# val = ' ' # THIS WORKS, BUT RESETS TO EMPTY CHOICE
self.color_options.SetLabel(val)
app = wx.App(0)
MyFrame1(None).Show()
app.MainLoop()
Basically, what I'm trying to do is get either a static label or read only textctrl to change it's price accordingly with the item selected in the choice widget. the lists for the Item_ID's and respective prices share the same indexes despite being on 2 different lists given their simultaneous creation... thing is no matter what Item I select, the price remains the same either way Code above has the static text method... the read only textctrl turned out to be the same any ideas on what can be done?
the code of the dialog in question
class AddItemOrder ( wx.Dialog ):
def __init__( self, parent,entryit ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Add Item", pos = wx.DefaultPosition, size = wx.Size( 285,173 ), style = wx.DEFAULT_DIALOG_STYLE )
self.entryit = entryit
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.ite = i.Item()
bSizer25 = wx.BoxSizer( wx.VERTICAL )
gSizer9 = wx.GridSizer( 2, 2, 0, 0 )
self.m_staticText34 = wx.StaticText( self, wx.ID_ANY, u"Item ID", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText34.Wrap( -1 )
gSizer9.Add( self.m_staticText34, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_choice2Choices = []
self.create_choices(self.m_choice2Choices)
self.Item_ID = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, self.m_choice2Choices, 0 )
self.Item_ID.SetSelection( 0 )
self.Item_ID.Bind(EVT_CHOICE,self.get_index)
gSizer9.Add( self.Item_ID, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_staticText17 = wx.StaticText( self, wx.ID_ANY, u"Item Price", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText17.Wrap( -1 )
gSizer9.Add( self.m_staticText17, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.Order_Price = wx.StaticText( self, wx.ID_ANY, str(self.prices[self.Item_ID.GetSelection() ]), wx.DefaultPosition, wx.DefaultSize, 0 )
self.Order_Price.Wrap( -1 )
self.Order_Price.SetLabel(str(self.prices[self.Item_ID.GetSelection() ]))
gSizer9.Add( self.Order_Price, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_staticText35 = wx.StaticText( self, wx.ID_ANY, u"Item Qty", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText35.Wrap( -1 )
gSizer9.Add( self.m_staticText35, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.Item_Qty = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 120,-1 ), 0 )
gSizer9.Add( self.Item_Qty, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
bSizer25.Add( gSizer9, 1, wx.EXPAND, 5 )
self.m_button19 = wx.Button( self, wx.ID_ANY, u"Ok", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_button19.Bind(wx.EVT_BUTTON, self.ok)
bSizer25.Add( self.m_button19, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )
self.SetSizer( bSizer25 )
self.Layout()
self.Centre( wx.BOTH )
the create choices and get index functions
def create_choices(self,event):
self.prices = []
for entry in self.ite.entries:
self.custom = entry.Item_ID
self.customPr = entry.Item_Price
self.prices.append(self.customPr)
self.m_choice2Choices.append(self.custom)
return self.m_choice2Choices
def get_index(self,event):
self.currIndex = self.Item_ID.GetSelection()
return self.currIndex
When you want to change the text shown by the statictext (presumably when the EVT_CHOICE handler in this case) then you just need to call the statictext's SetLabel method with the new text. So, something like this:
newText = str(self.prices[self.currIndex])
self.m_staticText17.SetLabel(newText)