python - wxPython: Using a GridBagSizer -
i beginner development in python, , i'm trying create simple application.
this application supposed show me frame containing gridbagsize
should load , position 4 buttons. however, small problem encounter 4 buttons found small in upper left of screen.
just clarify, i'm using python 2.7.8 windows 32-bit , wxpython 2.8.12.1.
i attaching code below:
#!/usr/bin/python # -*- coding: utf-8 -*- # import wx module import wx # creating class derived wx.frame class myframe(wx.frame): def __init__(self, title): super(myframe, self).__init__(parent=none, id=wx.id_any, title=title,style=wx.default_frame_style|wx.tab_traversal) # creating gridbagsizer framesizer=wx.gridbagsizer(vgap=5, hgap=5) # creating buttons inside frame , positioning them in gridbagsizer button1=wx.button(parent=self, id=wx.id_any, label="button 1") framesizer.add(item=button1, pos=(0, 0), span=(2, 1), flag=wx.align_centre) button2=wx.button(parent=self, id=wx.id_any, label="button 2") framesizer.add(item=button2, pos=(0, 1), span=(2, 1), flag=wx.align_centre) button3=wx.button(parent=self, id=wx.id_any, label="button 3") button4=wx.button(parent=self, id=wx.id_close, label="button 4") self.setsizer(framesizer) #framesizer.setsizehints(self) # set our frame dimansions self.setsize((400, 250)) # event of buttons ((only closing event button4) button4.bind(wx.evt_button, self.onclose) # creating class method associated action of button4 def onclose(self, evt): self.destroy() class myapp(wx.app): """ specific class application created """ def oninit(self): frame=myframe("example of small program") # display frame frame.show(true) # put frame in main window self.settopwindow(frame) return true # method of closing def onexit(self): result=wx.messagedialog(parent=none, message="goodbye", caption="exit", style=wx.ok) # display dialog goodbye result.showmodal() # program execution app=myapp(redirect=false) app.mainloop()
thank quick response.
yes, forgot add buttons 3 , 4 in framesizer.
i put insertion buttons code below.
at display, still have buttons reduced in upper left of screen.
about suggestion reduct span 1 row , 1 col, have same result.
i think have use addgrowablecol , addgrowablerow methods not know use them.
with gridsizer sizer, frame automatically resized.
apparently not case gridbagsizer?
thanks help.
best regards.
# creating buttons inside frame , positioning them in gridbagsizer button1=wx.button(parent=self, id=wx.id_any, label="button 1") framesizer.add(item=button1, pos=(0, 0), span=(2, 1), flag=wx.align_centre) button2=wx.button(parent=self, id=wx.id_any, label="button 2") framesizer.add(item=button2, pos=(0, 1), span=(2, 1), flag=wx.align_centre) button3=wx.button(parent=self, id=wx.id_any, label="button 3") framesizer.add(item=button3, pos=(2, 0), span=(2, 1), flag=wx.align_centre) button4=wx.button(parent=self, id=wx.id_close, label="button 4") framesizer.add(item=button4, pos=(2, 1), span=(2, 1), flag=wx.align_centre) self.setsizer(framesizer)
first, wanted thank you have given me on understanding of gridbagsizer.
i found solution problem.
in fact, used addgrowablerow , addgrowablecol methods.
i reaffixable entire code hereunder,hoping beginner me.
if find error in code, let me know.
best regards.
#!/usr/bin/python # -*- coding: utf-8 -*- # import wx module import wx # creating class derived wx.frame class myframe(wx.frame): def __init__(self, title): super(myframe, self).__init__(parent=none, id=wx.id_any, title=title,style=wx.default_frame_style|wx.tab_traversal) # creating gridbagsizer framesizer=wx.gridbagsizer(vgap=5, hgap=5) # creating buttons inside frame , positioning them in gridbagsizer button1=wx.button(parent=self, id=wx.id_any, label="button 1") framesizer.add(item=button1, pos=(0, 0), span=(2, 1), flag=wx.align_centre) button2=wx.button(parent=self, id=wx.id_any, label="button 2") framesizer.add(item=button2, pos=(0, 1), span=(2, 1), flag=wx.align_centre) button3=wx.button(parent=self, id=wx.id_any, label="button 3") framesizer.add(item=button3, pos=(2, 0), span=(2, 1), flag=wx.align_centre) button4=wx.button(parent=self, id=wx.id_close, label="button 4") framesizer.add(item=button4, pos=(2, 1), span=(2, 1), flag=wx.align_centre) self.setsizer(framesizer) framesizer.setsizehints(self) # resize our columns method addgrowablecol in range(2): framesizer.addgrowablecol(i) # resize our rows method addgrowablerow in range(4): framesizer.addgrowablerow(i) # set our frame dimansions self.setsize((400, 250)) # event of buttons ((only closing event button4) button4.bind(wx.evt_button, self.onclose) # creating class method associated action of button4 def onclose(self, evt): self.destroy() class myapp(wx.app): """ specific class application created """ def oninit(self): frame=myframe("example of small program") # display frame frame.show(true) # put frame in main window self.settopwindow(frame) return true # method of closing def onexit(self): result=wx.messagedialog(parent=none, message="goodbye", caption="exit", style=wx.ok) # display dialog goodbye result.showmodal() # program execution app=myapp(redirect=false) app.mainloop()
Comments
Post a Comment