user845459
user845459

Reputation:

Refreshing panel contents in wxPython

What is the approach to update widgets in a wxPanel based on events from other controls on same panel?

Scenario 1 is updating the list of a comboBox based on what has been selected from another comboBox , where both are in same panel.

Scenario 2 is showing a new control/widget in a panel based on an event.

Basically creating new controls is easy but I dont know how to refresh/update my panel so immedialtly shows them.

Upvotes: 2

Views: 1690

Answers (2)

joaquin
joaquin

Reputation: 85605

Scenario 1

To change the choices of a combobox self.cbx you can use any of the following methods:

self.cbx.SetItems(choices) where choices is the full list of choices.
self.cbx.SetString(n, string) that sets the string at position n.
InsertItems(items, pos) Inserts the list of strings in the items argument into the list box before the position in the pos argument.

Note that the method Set(choices) of listboxes does not exist for the list in comboboxes. You must use SetItems(choices) instead (this is not clearly indicated in some textbooks).

If you want these changes to occur as a result of a selection in another combobox self.cbx_1 , just get the event (self.Bind(wx.EVT_COMBOBOX, on_combo_1, self.cbx_1)) of the first combobox, process your data as you like in the corresponding self.on_combo method and use one of the above methods to modify the second combobox.

For example:

def on_combo_1(self, evt):
    "append cbx_1 selection to cbx if not already in cbx"
    selection = self.cbx_1.GetStringSelection()
    cbx_choices = self.cbx.GetItems()
    if selection not in cbx_choices:
        cbx_choices.append(selection)
        self.cbx.SetItems(cbx_choices)

The fact the comboboxes are in the same or different panel is irrelevant for that.

Scenario 2

Normally you put your widgets inside sizers. To hide or made visible elements on the sizer you call the methods Show, Hide or Layout:

Show(self, item, show=True, recursive=false)

Shows or hides an item managed by the sizer. To make a sizer item disappear or reappear, use Show followed by Layout. The item parameter can be either a window, a sizer, or the zero-based index of the item. Use the recursive parameter to show or hide an item in a subsizer. Returns True if the item was found.

Hide(self, item, recursive) 

A convenience method for Show (item, False, recursive).

Layout(self)

This method will force the recalculation and layout of the items controlled by the sizer using the current space allocated to the sizer. Normally this is called automatically from the owning window's EVT_SIZE handler, but it is also useful to call it from user code when one of the items in a sizer change size, or items are added or removed.

References: wxPython in Action, Noel Rappin and Robin Dunn

Upvotes: 1

Mike Driscoll
Mike Driscoll

Reputation: 33071

For scenario one, you'd do something like the following (assuming the first combobox is bound to its EVT_COMBOBOX:

value = self.cboOne.GetValue()
if value == "something":
    self.cboTwo.SetItems(someList)

For showing a new widget, you could create it and then use Show()/Hide() as necessary. If the widget is in a sizer, then use the Sizer's Append or Insert methods. It also has a Detach method that can be used to hide widgets or you just call Hide itself. See the documentation for more information: http://www.wxpython.org/docs/api/wx.Sizer-class.html

Upvotes: 0

Related Questions