Reputation: 331
In the code below I am trying to dynamically add new text controls to a sizer. I have two sizers, one on the left ("sizer") and one on the right ("sizer2"). There is a function called "add_region" which should add the new text control to the sizer on the right (sizer2).
I have bound this function to the button "button_add".
When I call the function add_region directly from the main body of the code ( line: self.add_region(0)
), the text control is added to sizer2 as desired. However, the same function is called by hitting the button, the text control is put in a different sizer (presumably "sizer").
Why does the position of the text control depend on the way in which the function is called?
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#This is a good tutorial: http://wiki.wxpython.org/wxPython%20by%20Example
import wx
import wx.lib.scrolledpanel
class region():
#variables associated with the entire class (not each object)
nreg=0
def __init__(self,Ani=999.0e29,AZ=9.99):
self.Ani=Ani
self.AZ=AZ
# self.text_Ani
region.nreg+=1
class MainWindow(wx.Frame): #A "frame" is what we normally think of as a window, so we have called it MainWindow
def __init__(self, parent, title):
super( MainWindow, self).__init__(parent, title=title, size=(450, 350) )
self.dirname = ''
self.ibox=0
self.iv=0 #the vertical depth on the LHS
self.iw=0 #the vertical depth on the RHS
self.Aregion_objects=[]
self.Ani=[] #array of ion number densities
self.AZ=[]
self.Axmin=[]
self.Axmax=[]
self.Aymin=[]
self.Aymax=[]
self.text_Ani=[]
self.tc_Ani=[]
self.sizer = wx.GridBagSizer(5, 1)
#also declare a sizer that will contain all the regions on the LHS (sizer2)
self.sizer2 = wx.GridBagSizer(1, 1)
#self.SetMinSize(self.GetSize()) #prevents the window from being made arbitrarily small
self.SetClientSize( (900,900) )
self.SetMinSize( (-1,-1) )
self.InitGUI()
self.Centre()
# self.SetupScrolling()
self.Show()
def InitGUI(self):
screenSize = wx.DisplaySize()
screenWidth = screenSize[0]
screenHeight = screenSize[1]
self.panel = wx.lib.scrolledpanel.ScrolledPanel(self)
# self.panel.Layout()
self.panel.SetupScrolling()
font_title = wx.Font(10, wx.DECORATIVE, wx.NORMAL, wx.BOLD)
#self.panel.SetBackgroundColour('#4f5049')
#a sizer is a way of laying out widgets, it is not a window in itself.
# This is why we create widgets in the self.panel (by using "self.panel" as the parent), then afterwards add the widget to the sizer.
sizer = self.sizer #wx.GridBagSizer(5, 1)
#also declare a sizer that will contain all the regions on the LHS (sizer2)
sizer2 = self.sizer2 #wx.GridBagSizer(1, 1)
# sizer = wx.BoxSizer()
#make a horizontal box dividing the page into left and RHS. We will put the gridbagsizer into the
#LHS and the target picture into the RHS of the hbox
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(sizer,2) #add the box to the current window (self)
#make a vertical box that will contain the stuff on the RHS
vbox_RHS = wx.BoxSizer(wx.VERTICAL)
hbox.Add(vbox_RHS,2) #add the vbox into the hbox, so it now takes up the 2nd position in the hbox (the gridbagsizer took the first)
text_view = wx.StaticText(self.panel, label="Target View")
text_view.SetFont(font_title)
vbox_RHS.Add(text_view, flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
self.iw+=1
button_redraw = wx.Button(self.panel, label="Redraw")
self.Bind(wx.EVT_BUTTON, self.redraw_pic, button_redraw)
vbox_RHS.Add(button_redraw)
self.iw+=1
self.pic = wx.StaticBitmap( self.panel, -1, wx.Bitmap("small1.jpg", wx.BITMAP_TYPE_ANY),size=(400,250) )
vbox_RHS.Add(self.pic)
self.iw+=1
vbox_RHS.Add(sizer2)
button_check = wx.Button(self.panel, label="Check")
self.Bind(wx.EVT_BUTTON, self.check_all, button_check)
sizer.Add(button_check, pos=(self.iv, 0))
self.iv+=1
text_PIC = wx.StaticText(self.panel, label="Main PIC Options")
text_PIC.SetFont(font_title)
sizer.Add(text_PIC, pos=(self.iv, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
self.iv+=1
self.tc_info = wx.TextCtrl(self.panel)
sizer.Add(self.tc_info, pos=(self.iv, 1), span=(1, 3), flag=wx.TOP)
self.iv+=1
self.add_region(0)
button_add = wx.Button(self.panel, label="Add Region")
self.Bind(wx.EVT_BUTTON, self.add_region, button_add)
vbox_RHS.Add(button_add)
self.iw+=1
sizer.AddGrowableCol(2)
self.panel.SetSizer(hbox)
# GUI EVENTS
def checkBtnClick(self, e):
self.tc_info.SetValue("blahh")
def check_all(self, e):
self.read_all()
#----------------------------------------------------------------------
def redraw_pic(self, e):
#blah
print "Redraw called"
def save_all(self, event):
saveFileDialog = wx.FileDialog(self, "Save As", "", "","Python files (*.py)|*.py",wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
saveFileDialog.ShowModal()
saveFileDialog.GetPath()
saveFileDialog.Destroy()
def read_all(self):
# self.ibox=0 #increases each time a new box is added
# self.nbox=0 #number of boxes currently on the page (accounts for fact some may have been deleted)
# self.Aid=[]
# self.Apresent=[]
self.l_hybrid=0
self.l_colls=0
self.l_implicit=0
self.l_damp_bounds=0
self.l_cold=0
self.l_laser_const=0
# self.nppc=self.tc_nppc.GetValue()
self.nx=self.tc_nx.GetValue()
self.nsp=self.tc_nsp.GetValue()
self.xmax=self.tc_xmax.GetValue()
self.nmin=self.tc_nmin.GetValue()
self.inten=self.tc_inten.GetValue()
self.Ti_init0=self.tc_Ti_init0.GetValue()
self.Ti_init1=self.tc_Ti_init1.GetValue()
self.mi=self.tc_mi.GetValue()
self.tout=self.tc_tout.GetValue()
self.gaussian_cutoffs=self.tc_gaussian_cutoffs.GetValue()
self.t_FWHM=self.tc_t_FWHM.GetValue()
print "nppc=",self.nppc
print "t_FWHM=",self.t_FWHM
# def add_region(self,e):
# #blah
# print "add called"
def add_region(self,event):
print "add_region called"
# i=self.ibox
i=region.nreg
sizer=self.sizer2
reg=region(Ani=0.0,AZ=0.0)
self.Aregion_objects.append(reg)
print "add_region:iw=",self.iw
font_title = wx.Font(10, wx.DECORATIVE, wx.NORMAL, wx.BOLD)
text_region = wx.StaticText(self.panel, label="Region "+str(i))
text_region.SetFont(font_title)
sizer.Add(text_region, pos=(self.iw, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
self.iw+=1
if __name__ == '__main__':
app = wx.App()
MainWindow(None, title="")
app.MainLoop()
Upvotes: 0
Views: 303
Reputation: 427
I din't really dig too much into this but I was able to fix it by adding:
self.panel.Layout()
event.Skip()
to the end of add_region
. The reason for this is because when you add things to a sizer, you have to tell it that the layout has changed, hence a call to self.panel.Layout
.(Notice how this is a call for the panel and not the Frame itself) class. The event.Skip()
tells wx to check if it needs to do anything else when a certain event type is launched, in this case it checks if it needs to anything else after the button is pushed.
One quick note that I should say is that it's best to separate the Frame and Panel instances to a class on their own. That way panels are potentially reusable, and more organized code plus best practice :p If your just messing around with wx then this is fine, but just a little note :)
Hope this helps
Upvotes: 1