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 the following Python code that would make a GUI for the command line process of turning a video into frames that is native to VLC Media Player.
# -*- coding: utf-8 -*-
from subprocess import call
import wx
import wx.xrc
vidpath = ''
framedir = ''
def VideoToFrame(videopath, targetdirpath):
"Converts a video into frames. Make sure you have VLC Media Player installed!"
call('vlc.exe "' + videopath + '" --video-filter=scene --vout=dummy --start-time=300 --stop-time=600 --scene-ratio=250 --scene-path=”' + targetdirpath + '” vlc://quit')
if __name__ == "__main__":
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( 436,159 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
gbSizer1 = wx.GridBagSizer( 0, 0 )
gbSizer1.SetFlexibleDirection( wx.BOTH )
gbSizer1.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
gbSizer2 = wx.GridBagSizer( 0, 0 )
gbSizer2.SetFlexibleDirection( wx.BOTH )
gbSizer2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.VidLabel = wx.StaticText( self, wx.ID_ANY, u"Video path", wx.DefaultPosition, wx.DefaultSize, 0 )
self.VidLabel.Wrap( -1 )
gbSizer2.Add( self.VidLabel, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_filePicker1 = wx.FilePickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE )
gbSizer2.Add( self.m_filePicker1, wx.GBPosition( 1, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
gbSizer1.Add( gbSizer2, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
gbSizer3 = wx.GridBagSizer( 0, 0 )
gbSizer3.SetFlexibleDirection( wx.BOTH )
gbSizer3.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.FrameLabel = wx.StaticText( self, wx.ID_ANY, u"Frame extraction path", wx.DefaultPosition, wx.DefaultSize, 0 )
self.FrameLabel.Wrap( -1 )
gbSizer3.Add( self.FrameLabel, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_dirPicker1 = wx.DirPickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select a folder", wx.DefaultPosition, wx.DefaultSize, wx.DIRP_DEFAULT_STYLE )
gbSizer3.Add( self.m_dirPicker1, wx.GBPosition( 1, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
gbSizer1.Add( gbSizer3, wx.GBPosition( 0, 1 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.framebutton = wx.Button( self, wx.ID_ANY, u"Extract frames!", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.framebutton, 0, wx.ALL, 5 )
gbSizer1.Add( bSizer1, wx.GBPosition( 2, 0 ), wx.GBSpan( 1, 1 ), wx.EXPAND, 5 )
self.SetSizer( gbSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_filePicker1.Bind( wx.EVT_FILEPICKER_CHANGED, self.chvidpath )
self.m_dirPicker1.Bind( wx.EVT_DIRPICKER_CHANGED, self.chframedir )
self.framebutton.Bind( wx.EVT_BUTTON, self.extractframes )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def chvidpath( self, event ):
vidpath = event.path
event.Skip()
def chframedir( self, event ):
framedir = event.path
event.Skip()
def extractframes( self, event ):
VideoToFrame(vidpath, framedir)
event.Skip()
Unfortunately I couldn't manage the frame to show when I ran the script.
The result was... nothing!
Please help! I have no idea of what I am doing, and no other idea of how to make a GUI of a command line process.
I am a good Python player though.
I don't see where you created an instance of your MyFrame1
add:
app = wx.App()
frame = MyFrame1(None)
frame.Show()
app.MainLoop()
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)