473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

wxPython, dynamically modify window

Hi, I am looking for a tip. I have a panel and a checkbox. When I
check the checkbox, I would like to add buttons to the panel
(dynamically). When the checkbox is unchecked, the buttons should not
appear (be deleted)---all the while, the window should resize if necessary.

If you have a simpler example, that is fine. I just need a hint as to
how you dynamically change the widgets and their layouts.

Looking at the wx demos, there is something close wx.lib.expando, but
this is just modifying a particular widget.

Thanks.
Dec 12 '06 #1
1 6054

Grant wrote:
Hi, I am looking for a tip. I have a panel and a checkbox. When I
check the checkbox, I would like to add buttons to the panel
(dynamically). When the checkbox is unchecked, the buttons should not
appear (be deleted)---all the while, the window should resize if necessary.
Here is one way that seems to work (wxPython 2.7, Python 2.5, Mac OS X
10.4.8). It uses a sizer's Hide() and Show() methods to control
visibility of a child sizer containing the buttons. To resize the frame
containing the checkbox and buttons, ensure the frame has a sizer and
use the Fit() method after Hiding and Showing the buttons.
HTH,
-- CMcP

import wx

class AppFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = wx.Panel(self, -1)
panel_sizer = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(panel_sizer)

cb = wx.CheckBox(panel, -1, 'Need some buttons')
cb.SetValue(False)
self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, cb)

button_sizer = wx.BoxSizer(wx.HORIZONTAL)
b1 = wx.Button(panel, -1, 'Button 1')
b2 = wx.Button(panel, -1, 'Button 2')
button_sizer.Add(b1, 0, wx.ALL, 5)
button_sizer.Add(b2, 0, wx.ALL, 5)

panel_sizer.Add(cb, 0, wx.ALL, 5)
panel_sizer.Add(button_sizer)

panel_sizer.Hide(button_sizer, recursive=True)

frame_sizer = wx.BoxSizer()
frame_sizer.Add(panel, 1, wx.EXPAND)

self.SetSizer(frame_sizer)
self.panel_sizer = panel_sizer
self.button_sizer = button_sizer

self.Fit()

def EvtCheckBox(self, event):
cb = event.GetEventObject()
if cb.GetValue() == True:
self.panel_sizer.Show(self.button_sizer, recursive=True)
else:
self.panel_sizer.Hide(self.button_sizer, recursive=True)
self.Fit()

class App(wx.App):
def OnInit(self):
frame = AppFrame(None, 'Hide/Show Example')
self.SetTopWindow(frame)
frame.Show()
return True

if __name__ == '__main__':
app = App()
app.MainLoop()

Dec 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Logan | last post by:
Would you recommend to use the wx package of wxPython? From the documentation: Provides a way to drop the wx prefix from wxPython objects by dynamically loading and renaming objects from the...
9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
0
by: Richard Townsend | last post by:
I've been experimenting with passing a window handle from a wxPython app to a Tkinter app. The Tkinter app should embed a Toplevel window containing a Canvas widget in the wxPython app's Frame (see...
0
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this...
0
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this...
4
by: stef | last post by:
hello, I'm trying to move from Delphi to Python (move from MatLab to Python already succeeded, also thanks to this discussion group). From the discussions in this list about "the best" GUI for...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
0
by: Guilherme Polo | last post by:
On 10/29/08, Olrik Lenstra <o.lenstra@gmail.comwrote: It will be a combination of commands, not a single one. Initially I considered this as "probably without solution", since tcl acquired a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.