wxPython. Create a panel with four static sized boxes - python

I'm trying to create a panel, with four boxes containing some data. These four boxes should have a predefined static size. What I have so far is four boxes that is overlapping to some extent.
Any ideas?
Code:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.pl = wx.Panel(self)
self.SetSize((500, 350))
sb = wx.StaticBox(self.pl, -1, 'BOX0', size=(180, 150))
sat = wx.CheckBox(self.pl, -1, 'Satellite')
gsm = wx.CheckBox(self.pl, -1, 'GSM')
wlan = wx.CheckBox(self.pl, -1, 'WLAN')
sb2 = wx.StaticBox(self.pl, -1, 'BOX1', size=(180, 150))
nm2 = wx.StaticText(self.pl, -1, 'default1')
sb3 = wx.StaticBox(self.pl, -1, 'BOX2', size=(180, 150))
nm3 = wx.StaticText(self.pl, -1, 'default2')
sb4 = wx.StaticBox(self.pl, -1, 'BOX3', size=(180, 150))
nm4 = wx.StaticText(self.pl, -1, 'default3')
box = wx.StaticBoxSizer(sb, wx.VERTICAL)
box.Add(sat, 0, wx.ALL, 5)
box.Add(gsm, 0, wx.ALL, 5)
box.Add(wlan, 0, wx.ALL, 5)
box2 = wx.StaticBoxSizer(sb2)
box2.Add(nm2, 0, wx.ALL, 5)
box3 = wx.StaticBoxSizer(sb3)
box3.Add(nm3, 0, wx.ALL, 5)
box4 = wx.StaticBoxSizer(sb4)
box4.Add(nm4, 0, wx.ALL, 5)
gs = wx.BoxSizer(wx.HORIZONTAL)
gs.Add(box)
gs.Add(box2)
gss = wx.BoxSizer(wx.HORIZONTAL)
gss.Add(box3)
gss.Add(box4)
gt = wx.BoxSizer(wx.VERTICAL)
gt.Add(gs)
gt.Add(gss)
self.pl.SetSizer(gt)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, '08_gridsizer.py')
frame.Show()
self.SetTopWindow(frame)
return 1
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()

I'll just answer my own question.
The solution is to add a wx.Sizer.SetMinSize() to each wx.StaticBoxSizer() like this.
sb = wx.StaticBox(self.pl, -1, 'BOX0')
sat = wx.CheckBox(self.pl, -1, 'Satellite')
gsm = wx.CheckBox(self.pl, -1, 'GSM')
wlan = wx.CheckBox(self.pl, -1, 'WLAN')
box = wx.StaticBoxSizer(sb, wx.VERTICAL)
box.SetMinSize((180, 150))
box.Add(sat, 0, wx.ALL, 5)
box.Add(gsm, 0, wx.ALL, 5)
box.Add(wlan, 0, wx.ALL, 5)
And I removed the size argument in wx.StaticText()

Related

Delete item from listBox wxpython

I have this class which deals with listBox:
I tried to make a delete button for listBox items but it didn't work. The "deleteItem" function does now work.Without the marked parts it works good, but there is no delete option. Help someone?
class SettingProcess(wx.Frame):
itemsArr= []
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Proceses",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1))
self.txt_processname.SetValue("chrome.exe")
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
global ProcessListMain
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, wx.ID_CLOSE, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, wx.ID_CLOSE, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
**m_delete = wx.Button(panel, wx.ID_CLOSE, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)**
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,str):
global ProcessListMain
#print "value is "+ str(self.txt_processname.GetValue())
ProcessListMain.append(self.txt_processname.GetValue())
self.files_listBox.Set(ProcessListMain)
def onExit(self, event):
self.Destroy()
***def deleteItem(self,event):
numOfItems=self.files_listBox.GetCount()
for i in range(numOfItems):
self.itemsArr[i]=self.files_listBox.GetString()
selectedItems=self.files_listBox.GetStringSelection()
self.files_listBox.clear()
for i in numOfItems :
if self.itemsArr[i]!=selectedItems:
self.files_listBox.append( self.itemsArr[i])***
Here is the version, courtesy of Robin Dunn's comment:
Note that I have also bound the textctrl so that just filling in a process name and pressing Enter, also acts like the Add button.
import wx
class SettingProcess(wx.Frame):
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Processes",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1), style=wx.TE_PROCESS_ENTER)
self.txt_processname.SetValue("chrome.exe")
self.txt_processname.Bind(wx.EVT_TEXT_ENTER, self.OnAddButton)
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
ProcessListMain = ['Process A','Process B','Process C','Process D','Process E']
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, -1, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, -1, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
m_delete = wx.Button(panel, -1, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,event):
Addproc = self.txt_processname.GetValue()
if Addproc:
self.files_listBox.Append(Addproc)
def onExit(self, event):
self.Destroy()
def deleteItem(self,event):
deleted_item = self.files_listBox.GetSelection()
self.files_listBox.Delete(deleted_item)
if __name__=='__main__':
app = wx.App()
frame = SettingProcess()
frame.Show()
app.MainLoop()
It's a bit more complicated than having duplicate Id's for your buttons, as I suggested earlier.
Try the following: (I have stuck with your global ProcessListMain but had to mock it up, as it isn't declared in your questions code)
import wx
class SettingProcess(wx.Frame):
itemsArr= []
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Black list Processes",size=(500, 600))
self.Centre()
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY,size=(500, 600))
self.index = 0
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
lbl_process = wx.StaticText(panel, -1, "Process name")
lbl_process.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.txt_processname = wx.TextCtrl(panel, size=(140, -1))
self.txt_processname.SetValue("chrome.exe")
sizer.AddMany([lbl_process, self.txt_processname])
#------------- Files ListBox -------------------------------------
global ProcessListMain
ProcessListMain = ['123','456','789','1123','2123']
self.files_listBox = wx.ListBox(panel, -1, (12, 130), (200, 240), ProcessListMain,
wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_ALWAYS_SB | wx.LB_SORT)
self.files_listBox.SetBackgroundColour(wx.Colour(255, 255, 128))
#------------- Set event handlers ---------------------------------
m_start = wx.Button(panel, -1, "Add")
m_start.Bind(wx.EVT_BUTTON, self.OnAddButton)
sizer.Add(m_start, 0, wx.ALL, 10)
m_close = wx.Button(panel, -1, "Close")
m_close.Bind(wx.EVT_BUTTON, self.onExit)
sizer.Add(m_close, 0, wx.ALL, 10)
m_delete = wx.Button(panel, -1, "Delete")
m_delete.Bind(wx.EVT_BUTTON, self.deleteItem)
sizer.Add(m_delete, 0, wx.ALL, 10)
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def OnAddButton(self,event):
global ProcessListMain
Addproc = self.txt_processname.GetValue()
ProcessListMain.append(Addproc)
self.files_listBox.Set(ProcessListMain)
def onExit(self, event):
self.Destroy()
def deleteItem(self,event):
global ProcessListMain
deleted_item = self.files_listBox.GetStringSelection()
numOfItems=self.files_listBox.GetCount()
itemsArr = []
for i in range(numOfItems):
x = self.files_listBox.GetString(i)
if x != deleted_item:
itemsArr.append(x)
ProcessListMain = itemsArr
self.files_listBox.Clear()
self.files_listBox.Set(ProcessListMain)
self.files_listBox.Update()
if __name__=='__main__':
app = wx.App()
frame = SettingProcess()
frame.Show()
app.MainLoop()

wxPython layout using boxsizers

I've recently started using wxPython to build a GUI and I'm trying to create the following layout:
Button1 Button2 Button3
----------------------------------------
listbox | textctrl
The buttons should have a flexible width, expanding to fill the full width of the frame with a border between them (each buttons has a width (incl. border) of 1/3 frame). Their height should be set to a height in pixels.
The listbox should fill the frame vertically and have a set width of x pixels
The textctrol should be a textbox which expands to fill the width of the frame vertically as well as horizontally.
This is the code I have:
mainPanel = wx.Panel(self, -1)
parentBox = wx.BoxSizer(wx.VERTICAL)
menubar = wx.MenuBar()
filem = wx.Menu()
menubar.Append(filem, '&File')
self.SetMenuBar(menubar)
navPanel = wx.Panel(mainPanel, -1, size=(1000, 80))
navBox = wx.BoxSizer(wx.HORIZONTAL)
newSection = wx.Button(navPanel, self.ID_NEW, 'New')
renSection = wx.Button(navPanel, self.ID_RENAME, 'Rename')
dltSection = wx.Button(navPanel, self.ID_DELETE, 'Delete')
navBox.Add(newSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(renSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(dltSection, 1, wx.EXPAND | wx.ALL, 5)
navPanel.SetSizer(navBox)
contentPanel = wx.Panel(mainPanel, -1, size=(1000, 600))
contentBox = wx.BoxSizer(wx.HORIZONTAL)
self.listbox = wx.ListBox(contentPanel, -1, size=(300, 700))
self.settings = wx.TextCtrl(contentPanel, -1)
contentBox.Add(self.listbox, 0)
contentBox.Add(self.settings, 1, wx.EXPAND | wx.ALL, 5)
contentPanel.SetSizer(contentBox)
parentBox.Add(navPanel, 0, wx.EXPAND | wx.ALL, 5)
parentBox.Add(contentPanel, 1, wx.EXPAND | wx.ALL, 5)
mainPanel.SetSizer(parentBox)
Something is going wrong since what I see is not what I expect to see, anybody who can help me out?
It is working for me, I'm on win64, python 32bit 2.7.3.3, wx '2.8.12.1 (msw-unicode)'. The full working test example is:
import wx
class testframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'some title')
mainPanel = wx.Panel(self, -1)
parentBox = wx.BoxSizer(wx.VERTICAL)
menubar = wx.MenuBar()
filem = wx.Menu()
menubar.Append(filem, '&File')
self.SetMenuBar(menubar)
navPanel = wx.Panel(mainPanel, -1, size=(1000, 80))
navBox = wx.BoxSizer(wx.HORIZONTAL)
newSection = wx.Button(navPanel, -1, 'New')
renSection = wx.Button(navPanel, -1, 'Rename')
dltSection = wx.Button(navPanel, -1, 'Delete')
navBox.Add(newSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(renSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(dltSection, 1, wx.EXPAND | wx.ALL, 5)
navPanel.SetSizer(navBox)
contentPanel = wx.Panel(mainPanel, -1, size=(1000, 600))
contentBox = wx.BoxSizer(wx.HORIZONTAL)
self.listbox = wx.ListBox(contentPanel, -1, size=(300, 700))
self.settings = wx.TextCtrl(contentPanel, -1)
contentBox.Add(self.listbox, 0, wx.ALL, 5)
contentBox.Add(self.settings, 1, wx.EXPAND | wx.ALL, 5)
contentPanel.SetSizer(contentBox)
parentBox.Add(navPanel, 0, wx.EXPAND | wx.ALL, 5)
parentBox.Add(contentPanel, 1, wx.EXPAND | wx.ALL, 5)
mainPanel.SetSizer(parentBox)
parentBox.Fit(self)
app = wx.PySimpleApp()
app.frame = testframe()
app.frame.Show()
app.MainLoop()
Notice the addition of Fit() of the main sizer, and also 5px border added to the listbox.

Trouble with wxpython when using sizer with checklistbox

When I try to use a sizer with the checklist box everything overlaps terribly. My goal is to have the checklistbox on the left and the input fields on the right of the window. I do not know whether the sizer is the solution but I thought it should offer a way to keep the checklistbox on the left and the input fields on the right. Ideally I would also have the ok button and cancel button centered underneath everything else.
import wx
import wx.lib.scrolledpanel
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='My Form')
# Add a panel so it looks correct on all platforms
self.panel = wx.Panel(self, wx.ID_ANY)
# panel2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(200,400), pos=(0,28), style=wx.SIMPLE_BORDER)
# panel2.SetupScrolling()
# bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_OTHER, (16, 16))
# titleIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
title = wx.StaticText(self.panel, wx.ID_ANY, 'My Title')
# bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_OTHER, (16, 16))
# inputOneIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
labelOne = wx.StaticText(self.panel, wx.ID_ANY, 'Input 1')
inputTxtOne = wx.TextCtrl(self.panel, wx.ID_ANY, '')
# inputTwoIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
labelTwo = wx.StaticText(self.panel, wx.ID_ANY, 'Input 2')
inputTxtTwo = wx.TextCtrl(self.panel, wx.ID_ANY,'')
# inputThreeIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
# labelThree = wx.StaticText(self.panel, wx.ID_ANY, 'Input 3')
# inputTxtThree = wx.TextCtrl(self.panel, wx.ID_ANY, '')
# inputFourIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
# labelFour = wx.StaticText(self.panel, wx.ID_ANY, 'Input 4')
# inputTxtFour = wx.TextCtrl(self.panel, wx.ID_ANY, '')
okBtn = wx.Button(self.panel, wx.ID_ANY, 'OK')
cancelBtn = wx.Button(self.panel, wx.ID_ANY, 'Cancel')
self.Bind(wx.EVT_BUTTON, self.onOK, okBtn)
self.Bind(wx.EVT_BUTTON, self.onCancel, cancelBtn)
finalSizer = wx.BoxSizer(wx.HORIZONTAL)
topSizer = wx.BoxSizer(wx.VERTICAL)
titleSizer = wx.BoxSizer(wx.HORIZONTAL)
inputOneSizer = wx.BoxSizer(wx.HORIZONTAL)
inputTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
checkSizer = wx.BoxSizer(wx.HORIZONTAL)
# inputThreeSizer = wx.BoxSizer(wx.HORIZONTAL)
# inputFourSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
# titleSizer.Add(titleIco, 0, wx.ALL, 5)
titleSizer.Add(title, 0, wx.ALL, 5)
# inputOneSizer.Add(inputOneIco, 0, wx.ALL, 5)
inputOneSizer.Add(labelOne, 0, wx.ALL, 5)
inputOneSizer.Add(inputTxtOne, 1, wx.ALL|wx.EXPAND, 5)
# inputTwoSizer.Add(inputTwoIco, 0, wx.ALL, 5)
inputTwoSizer.Add(labelTwo, 0, wx.ALL, 5)
inputTwoSizer.Add(inputTxtTwo, 1, wx.ALL|wx.EXPAND, 5)
# inputThreeSizer.Add(inputThreeIco, 0, wx.ALL, 5)
# inputThreeSizer.Add(labelThree, 0, wx.ALL, 5)
# inputThreeSizer.Add(inputTxtThree, 1, wx.ALL|wx.EXPAND, 5)
# inputFourSizer.Add(inputFourIco, 0, wx.ALL, 5)
# inputFourSizer.Add(labelFour, 0, wx.ALL, 5)
# inputFourSizer.Add(inputTxtFour, 1, wx.ALL|wx.EXPAND, 5)
sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen']
lb = wx.CheckListBox(self, -1, (10, 10), (200,400), sampleList)
self.Bind(wx.EVT_LISTBOX, self.EvtListBox, lb)
self.Bind(wx.EVT_CHECKLISTBOX, self.EvtCheckListBox, lb)
lb.SetSelection(0)
self.lb = lb
checkSizer.Add(lb, 0, wx.ALL |wx.EXPAND | wx.LEFT, 5)
checkSizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 5)
btnSizer.Add(okBtn, 0, wx.ALL, 5)
btnSizer.Add(cancelBtn, 0, wx.ALL, 5)
topSizer.Add(titleSizer, 0, wx.CENTER)
# topSizer.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(inputOneSizer, 0, wx.ALL|wx.EXPAND | wx.RIGHT, 5)
topSizer.Add(inputTwoSizer, 0, wx.ALL|wx.EXPAND | wx.RIGHT, 5)
# topSizer.Add(inputThreeSizer, 0, wx.ALL|wx.EXPAND, 5)
# topSizer.Add(inputFourSizer, 0, wx.ALL|wx.EXPAND, 5)
# topSizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(btnSizer, 0, wx.ALL|wx.CENTER, 5)
# finalSizer.Add(lb, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
finalSizer.Add(checkSizer, 1, wx.EXPAND | wx.LEFT, 5)
# finalSizer.Add(inputTwoSizer, 1, wx.EXPAND | wx.LEFT, 5)
finalSizer.Add(topSizer, 1, wx.EXPAND | wx.RIGHT, 5)
self.panel.SetSizer(finalSizer)
finalSizer.Fit(self)
def EvtListBox(self, event):
self.log.WriteText('EvtListBox: %s\n' % event.GetString())
def EvtCheckListBox(self, event):
index = event.GetSelection()
label = self.lb.GetString(index)
status = 'un'
if self.lb.IsChecked(index):
status = ''
self.log.WriteText('Box %s is %schecked \n' % (label, status))
self.lb.SetSelection(index) # so that (un)checking also selects (moves the highlight)
def onOK(self, event):
# Do something
print 'onOK handler'
def onCancel(self, event):
self.closeProgram()
def closeProgram(self):
self.Close()
# Run the program
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyForm().Show()
# import wx.lib.inspection
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
I have found its easier to seperate out concerns
import wx
import wx.lib.scrolledpanel
class SizerMixin:
def _hz(self,*widgets):
sz = wx.BoxSizer(wx.HORIZONTAL)
sz.AddMany(widgets)
return sz
def _label(self,label_text,*widgets,**kwargs):
label_size = kwargs.pop("label_size",100)
return self._hz(
(wx.StaticText(self,-1,label_text,size=(label_size,-1)),0,wx.ALIGN_CENTER_VERTICAL),
*widgets)
class MainPanel(wx.Panel,SizerMixin):
def __init__(self,parent):
wx.Panel.__init__(self,parent,-1)
self.CreateWidgets()
self.SetSizer(self.DoLayout())
self.Fit()
def DoLayout(self):
sb = wx.StaticBox(self,-1,"Main Title")
sz = wx.StaticBoxSizer(sb,wx.VERTICAL)
title = wx.StaticText(self,-1,"A Title Goes Here")
sz.Add(title,0,wx.ALIGN_CENTER|wx.EXPAND)
sz.Add(wx.StaticLine(self,-1),0,wx.EXPAND,5) #seperator
body_sizer = wx.BoxSizer(wx.HORIZONTAL)
rightSideInputs = wx.BoxSizer(wx.VERTICAL)
rightSideInputs.Add(self._label("Input 1:",self._widgets["input1"]),0,wx.EXPAND|wx.ALL,5)
rightSideInputs.Add(self._label("Input 2:",self._widgets["input2"]),0,wx.EXPAND|wx.ALL,5)
body_sizer.Add(self._widgets["checklist"],1,wx.EXPAND)
body_sizer.Add(rightSideInputs,1,wx.EXPAND)
sz.Add(body_sizer)
button_sizer = wx.StdDialogButtonSizer()
ok_btn = wx.Button(self,wx.ID_OK)
cancel_btn = wx.Button(self,wx.ID_CANCEL)
button_sizer.AddButton(ok_btn)
button_sizer.AddButton(cancel_btn)
button_sizer.Realize()
sz.Add(button_sizer)
return sz
def CreateWidgets(self):
sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen']
self._widgets = {
"input1":wx.TextCtrl(self,-1),
"input2":wx.TextCtrl(self,-1),
"checklist":wx.CheckListBox(self,-1,choices=sampleList)
}
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='My Form')
self.panel = MainPanel(self)
self.Layout()
self.Fit()
def EvtListBox(self, event):
self.log.WriteText('EvtListBox: %s\n' % event.GetString())
def EvtCheckListBox(self, event):
index = event.GetSelection()
label = self.lb.GetString(index)
status = 'un'
if self.lb.IsChecked(index):
status = ''
self.log.WriteText('Box %s is %schecked \n' % (label, status))
self.lb.SetSelection(index) # so that (un)checking also selects (moves the highlight)
def onOK(self, event):
# Do something
print 'onOK handler'
def onCancel(self, event):
self.closeProgram()
def closeProgram(self):
self.Close()
# Run the program
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyForm().Show()
# import wx.lib.inspection
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()

Get variables from the GUI in program

first, sorry for my english - im still learning. I´m a student from Germany and i learn Python.
I have a program which needs a lot of paramters for running so i build a gui by wxGlade. Now i want to get this paramters in my application. I saw some things. They used the GUI for editing a INI- File. And the application gets the paramters from these INI. But this is not what i want. I want to controll my application with the GUI.
And it is very Important that i can save my Values in the GUI (so that the User should not do everything again).
Hope you understand what i mean.
Here is my Code for the Gui (not ready but it is enough for doing the first steps)
Here is my GUI Code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 (standalone edition) on Thu Apr 24 12:36:34 2014
#
import wx
# begin wxGlade: dependencies
import gettext
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
# Menu Bar
self.frame_3_menubar = wx.MenuBar()
wxglade_tmp_menu = wx.Menu()
wxglade_tmp_menu.Append(wx.ID_ANY, _("Beenden"), "", wx.ITEM_NORMAL)
self.frame_3_menubar.Append(wxglade_tmp_menu, _("Datei"))
wxglade_tmp_menu = wx.Menu()
self.frame_3_menubar.Append(wxglade_tmp_menu, _("Bearbeiten"))
wxglade_tmp_menu = wx.Menu()
wxglade_tmp_menu.Append(wx.ID_ANY, _("Dokumenationen"), "", wx.ITEM_NORMAL)
self.frame_3_menubar.Append(wxglade_tmp_menu, _("Hilfe"))
self.SetMenuBar(self.frame_3_menubar)
# Menu Bar end
self.frame_3_statusbarr = self.CreateStatusBar(1, 0)
self.kartei = wx.Notebook(self, wx.ID_ANY, style=0)
self.pane_all_settings = wx.Panel(self.kartei, wx.ID_ANY)
self.label_5 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Laufzeiteinstellungen"))
self.label_6 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Abrechnungsjahr"))
self.abr_jahr = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "", min=2000, max=2099, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL)
self.label_7 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Abrechnungmonat"))
self.abr_monat = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "", min=1, max=12)
self.label_8 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Payroll"))
self.payroll = wx.ComboBox(self.pane_all_settings, wx.ID_ANY, choices=[_("Loga"), _("Sage"), _("SAP"), _("KidiCap"), _("fidelis Personal")], style=wx.CB_DROPDOWN)
self.label_1 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Mandant"))
self.mandant = wx.SpinCtrl(self.pane_all_settings, wx.ID_ANY, "1", min=0, max=999, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL)
self.zuschlag = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _(u"Zuschl\xe4ge"))
self.fehlzeit = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _("Fehlzeiten"))
self.urlaub = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _(u"Urlaubsanspr\xfcche"))
self.soll = wx.CheckBox(self.pane_all_settings, wx.ID_ANY, _("Sollstunden"))
self.label_8_copy_1 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("ImpVar"))
self.dir_impvar = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "")
self.label_8_copy_2 = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("ImpUbr"))
self.dir_impubr = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "")
self.label_8_copy_1_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Pfad zur ImpVar"))
self.dir_impvar_copy = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "")
self.label_8_copy_1_copy_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Pfad zur ImpUbr"))
self.dir_impvar_copy_1 = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "")
self.label_8_copy_1_copy_copy_copy = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Ausgabeverzeichnis"))
self.dir_impvar_copy_2 = wx.TextCtrl(self.pane_all_settings, wx.ID_ANY, "")
self.button_1 = wx.Button(self.pane_all_settings, wx.ID_ANY, _("Exportieren"))
self.button_1_copy = wx.Button(self.pane_all_settings, wx.ID_ANY, _("Abbrechen"))
self.pan_loga = wx.Panel(self.kartei, wx.ID_ANY)
self.label_5_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Exportieren nach Loga"))
self.label_6_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Loga-Mandant"))
self.loga_mandant = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "1", min=0, max=1000000, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL)
self.label_7_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Loga-Abrechnungskreis"))
self.loga_al = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "", min=0, max=100)
self.label_8_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _("Empty"))
self.combo_box_1_copy = wx.ComboBox(self.pan_loga, wx.ID_ANY, choices=[], style=wx.CB_DROPDOWN)
self.label_1_copy = wx.StaticText(self.pan_loga, wx.ID_ANY, _(u"Personalnummer f\xfcllen"))
self.loga_fill_pnr = wx.SpinCtrl(self.pan_loga, wx.ID_ANY, "1", min=0, max=999, style=wx.SP_ARROW_KEYS | wx.TE_AUTO_URL)
self.konv_loa = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Konvertierungslohnart"))
self.konv_fehl = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Konvertierungsfehlzeiten"))
self.zeitraum_fehl = wx.CheckBox(self.pan_loga, wx.ID_ANY, _("Zeitraum Fehlzeit"))
self.vertragsnummer = wx.CheckBox(self.pan_loga, wx.ID_ANY, _(u"Vertragsnummer ber\xfccksichtigen"))
self.notebook_2_pane_3 = wx.Panel(self.kartei, wx.ID_ANY)
self.notebook_2_pane_4 = wx.Panel(self.kartei, wx.ID_ANY)
self.notebook_2_pane_5 = wx.Panel(self.kartei, wx.ID_ANY)
self.notebook_2_pane_6 = wx.Panel(self.kartei, wx.ID_ANY)
self.notebook_2_pane_7 = wx.Panel(self.kartei, wx.ID_ANY)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_MENU, self.stopExport, id=wx.ID_ANY)
self.Bind(wx.EVT_BUTTON, self.startExport, self.button_1)
self.Bind(wx.EVT_BUTTON, self.stopExport, self.button_1_copy)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle(_("TDA Export Manager 0.12"))
self.frame_3_statusbarr.SetStatusWidths([-1])
# statusbar fields
frame_3_statusbarr_fields = [_("(C) TDA-HR-Software Entwicklungs GmbH")]
for i in range(len(frame_3_statusbarr_fields)):
self.frame_3_statusbarr.SetStatusText(frame_3_statusbarr_fields[i], i)
self.payroll.SetSelection(-1)
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
grid_sizer_2_copy = wx.FlexGridSizer(10, 4, 0, 0)
grid_sizer_2 = wx.FlexGridSizer(10, 4, 0, 0)
grid_sizer_2.Add(self.label_5, 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_6, 0, wx.ALL, 10)
grid_sizer_2.Add(self.abr_jahr, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_7, 0, wx.ALL, 10)
grid_sizer_2.Add(self.abr_monat, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8, 0, wx.ALL, 10)
grid_sizer_2.Add(self.payroll, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_1, 0, wx.ALL, 10)
grid_sizer_2.Add(self.mandant, 0, wx.ALL, 10)
grid_sizer_2.Add(self.zuschlag, 0, wx.ALL, 10)
grid_sizer_2.Add(self.fehlzeit, 0, wx.ALL, 10)
grid_sizer_2.Add(self.urlaub, 0, wx.ALL, 10)
grid_sizer_2.Add(self.soll, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8_copy_1, 0, wx.ALL, 10)
grid_sizer_2.Add(self.dir_impvar, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8_copy_2, 0, wx.ALL, 10)
grid_sizer_2.Add(self.dir_impubr, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8_copy_1_copy, 0, wx.ALL, 10)
grid_sizer_2.Add(self.dir_impvar_copy, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8_copy_1_copy_copy, 0, wx.ALL, 10)
grid_sizer_2.Add(self.dir_impvar_copy_1, 0, wx.ALL, 10)
grid_sizer_2.Add(self.label_8_copy_1_copy_copy_copy, 0, wx.ALL, 10)
grid_sizer_2.Add(self.dir_impvar_copy_2, 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2.Add(self.button_1, 0, wx.ALL, 10)
grid_sizer_2.Add(self.button_1_copy, 0, wx.ALL, 10)
self.pane_all_settings.SetSizer(grid_sizer_2)
grid_sizer_2_copy.Add(self.label_5_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.label_6_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.loga_mandant, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.label_7_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.loga_al, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.label_8_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.combo_box_1_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.label_1_copy, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.loga_fill_pnr, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.konv_loa, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.konv_fehl, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.zeitraum_fehl, 0, wx.ALL, 10)
grid_sizer_2_copy.Add(self.vertragsnummer, 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
grid_sizer_2_copy.Add((20, 20), 0, wx.ALL, 10)
self.pan_loga.SetSizer(grid_sizer_2_copy)
self.kartei.AddPage(self.pane_all_settings, _("Allgemeine Einstellungen"))
self.kartei.AddPage(self.pan_loga, _("Loga"))
self.kartei.AddPage(self.notebook_2_pane_3, _("Sage"))
self.kartei.AddPage(self.notebook_2_pane_4, _("SAP"))
self.kartei.AddPage(self.notebook_2_pane_5, _("KidiCap"))
self.kartei.AddPage(self.notebook_2_pane_6, _("fidelis Personal"))
self.kartei.AddPage(self.notebook_2_pane_7, _("Konvertierungsfehlzeiten"))
sizer_2.Add(self.kartei, 1, wx.EXPAND, 0)
self.SetSizer(sizer_2)
sizer_2.Fit(self)
self.Layout()
# end wxGlade
def startExport(self, event): # wxGlade: MyFrame.<event_handler>
abrjahr = self.abr_jahr.GetValue()
print abrjahr
def stopExport(self, event): # wxGlade: MyFrame.<event_handler>
self.Close()
# end of class MyFrame
if __name__ == "__main__":
gettext.install("app") # replace with the appropriate catalog name
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_3 = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(frame_3)
frame_3.Show()
app.MainLoop()
There are ways to get the class objects and iterate over them.
But i'd suggest a restructure of the code and consider a container for your graphical elements instead, like this:
self.frames = {}
self.frames['_3_menubar'] = wx.MenuBar()
self.frames['frame_3_statusbarr'] = self.CreateStatusBar(1, 0)
self.frames['kartei'] = wx.Notebook(self, wx.ID_ANY, style=0)
self.frames['pane_all_settings'] = wx.Panel(self.kartei, wx.ID_ANY)
self.frames['label_5'] = wx.StaticText(self.pane_all_settings, wx.ID_ANY, _("Laufzeiteinstellungen"))
#... Etc etc, the rest of the objects
# And do something like this not only for your own sake
# But for getting rid huge amounts of code that can be compressed into stuff like this.
for item in ('Beenden', 'Datei', 'Bearbeiten', 'Dokumentationen', 'Hilfe'):
self.frames['_3_menubar'].Append(wxglade_tmp_menu, _(item))
With this you can simply do:
def getAllValues(self):
for frame in self.frames:
value = self.getValue(self.frames[frame])
yield (frame, value)
Or something similar, not sure what you want to do with the values but you could do:
def saveAllValues(self):
with open('settings.ini', 'w') as fh:
for frame, value in self.getAllValues():
fh.write(frame + ':' + str(value) + '\n')
I would suggest you do the following:
Use a dictionary to receive all the initial values. Let the names in the dictionary map to the names of your variables.
Allow other modules to subscribe to this GUI using the Observer Pattern.
When anything changes, or when the buttons are pressed, create a dictionary with the changes and notify all subscribers of the change.
This keeps the GUI cleanly separated from the rest of the program.

Graphics issue upon resizing a panel in wxPython

I am working with python v2.7 and wxPython v3.0 on Windows 8 OS.
(Sorry for the confusing title. I have no idea what the title should be. I shall try my best to explain my problem in detail.)
In my app I have a scrolled panel with a background image. This panel is named as mainPanel in the code snippet. This mainPanelcontains other panels named as myPanelA and myPanelB that have transparent background. These panels myPanelA and myPanelB contains sizers that are containing some buttons. PS: This is just a sample of my real world app. In my real world app I have many different panels and buttons. In this case the patches are more big and annoying. :(
Problem: When I resize my windows horizontally, some times (In my real world app it is very frequent as compared to this sample app.) I see blank patches in my App windows as shown in sample images below. How can I avoid this? It would be great if some one can test this and report if they have same problem on their machine (Just to be sure this is not Windows OS issue. I tested on Windows 7 too, it has the same problem.)
Update: Simply resize the window and scroll vertically, you'll see the patches quite often.
Sample Images: The patch is pointed by an arrow.
Code: Here is my code sample for playing around. The background image can be downloaded from here. greensquares.jpg
import wx
import wx.lib.scrolledpanel
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
mainPanel.SetupScrolling()
myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
wx.StaticText(myPanelA, -1, ' MyPanelA ')
myButton1A = wx.Button(myPanelA, -1, size=(20,20))
myButton2A = wx.Button(myPanelA, -1, size=(20,20))
mySizerA = wx.BoxSizer(wx.HORIZONTAL)
mySizerA.Add(myButton1A, 0, wx.ALL, 20)
mySizerA.Add(myButton2A, 0, wx.ALL, 20)
myPanelA.SetSizer(mySizerA)
myPanelA.Layout()
myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
wx.StaticText(myPanelB, -1, ' MyPanelB ')
myButton1B = wx.Button(myPanelB, -1, size=(20,20))
myButton2B = wx.Button(myPanelB, -1, size=(20,20))
myButton3B = wx.Button(myPanelB, -1, size=(20,20))
myButton4B = wx.Button(myPanelB, -1, size=(20,20))
mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
myButton5B = wx.Button(myPanelB, -1, size=(20,20))
myButton6B = wx.Button(myPanelB, -1, size=(20,20))
myButton7B = wx.Button(myPanelB, -1, size=(20,20))
myButton8B = wx.Button(myPanelB, -1, size=(20,20))
mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
mySizerC = wx.BoxSizer(wx.VERTICAL)
mySizerC.Add(mySizerB1)
mySizerC.Add(mySizerB2)
myPanelB.SetSizer(mySizerC)
myPanelB.Layout()
if __name__ == '__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="My-App")
frame.Show()
app.MainLoop()
Thank you for your time.
I added a Update method to your panels, which seems to have fixed the issue on my side.
import wx
import wx.lib.scrolledpanel
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
mainPanel.SetupScrolling()
myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
self.myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
wx.StaticText(self.myPanelA, -1, ' self.myPanelA ')
myButton1A = wx.Button(self.myPanelA, -1, size=(20,20))
myButton2A = wx.Button(self.myPanelA, -1, size=(20,20))
mySizerA = wx.BoxSizer(wx.HORIZONTAL)
mySizerA.Add(myButton1A, 0, wx.ALL, 20)
mySizerA.Add(myButton2A, 0, wx.ALL, 20)
self.myPanelA.SetSizer(mySizerA)
self.myPanelA.Layout()
self.myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
wx.StaticText(self.myPanelB, -1, ' self.myPanelB ')
myButton1B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton2B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton3B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton4B = wx.Button(self.myPanelB, -1, size=(20,20))
mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
myButton5B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton6B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton7B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton8B = wx.Button(self.myPanelB, -1, size=(20,20))
mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
mySizerC = wx.BoxSizer(wx.VERTICAL)
mySizerC.Add(mySizerB1)
mySizerC.Add(mySizerB2)
self.myPanelB.SetSizer(mySizerC)
self.myPanelB.Layout()
self.Bind(wx.EVT_SIZE, self.OnResize)
def OnResize(self, e):
self.myPanelA.Update()
self.myPanelB.Update()
e.Skip()
if __name__ == '__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="My-App")
frame.Show()
app.MainLoop()
I finally found the solution. In addition to what multiphrenic has suggested I have also binded the scroll event to a the OnResize(). So, that when ever any scroll event EVT_SCROLL occurs the panels will be updated too. It is working fine on Windows7, 8 OS with python v2.7 and wxPython v3.0.
Working code:
import wx
import wx.lib.scrolledpanel
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
mainPanel.SetupScrolling()
myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
self.myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
wx.StaticText(self.myPanelA, -1, 'self.myPanelA')
myButton1A = wx.Button(self.myPanelA, -1, size=(20,20))
myButton2A = wx.Button(self.myPanelA, -1, size=(20,20))
mySizerA = wx.BoxSizer(wx.HORIZONTAL)
mySizerA.Add(myButton1A, 0, wx.ALL, 20)
mySizerA.Add(myButton2A, 0, wx.ALL, 20)
self.myPanelA.SetSizer(mySizerA)
self.myPanelA.Layout()
self.myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
wx.StaticText(self.myPanelB, -1, 'self.myPanelB')
myButton1B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton2B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton3B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton4B = wx.Button(self.myPanelB, -1, size=(20,20))
mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
myButton5B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton6B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton7B = wx.Button(self.myPanelB, -1, size=(20,20))
myButton8B = wx.Button(self.myPanelB, -1, size=(20,20))
mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
mySizerC = wx.BoxSizer(wx.VERTICAL)
mySizerC.Add(mySizerB1)
mySizerC.Add(mySizerB2)
self.myPanelB.SetSizer(mySizerC)
self.myPanelB.Layout()
self.Bind(wx.EVT_SIZE, self.OnResize)
self.Bind(wx.EVT_SCROLL, self.OnResize)
def OnResize(self, e):
self.myPanelA.Update()
self.myPanelB.Update()
e.Skip()
if __name__ == '__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="My-App")
frame.Show()
app.MainLoop()

Categories

Resources