質問

I've been wondering whether it's possible to fill the wx.Choice widget on the fly (when clicking on it and before the options are shown).

I have the following use case for it: I have a form, where user needs to select an option from a wx.Choice, which is filled with data from a database. He discovers that the option he needs is absent there (so it's also absent in the db). Next, he minimises the window, goes to another one, from which he adds new data to the db, comes back and wants to select in the choice widget the newly typed data.

A simple solution would be to close the window and open it again, however it's not convenient. Is it possible to catch a click on event for the choice widget?

役に立ちましたか?

解決

There's no need to reinitialize the whole window; you can use Clear()', Append(string item) and AppendItems(list items) on your wx.Choice.

Let's create wx.Choice control and modify it's items later:

import wx

class myFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Code Example', size=(300, 200))
        panel = wx.Panel(self, -1)
        sampleList = ['A', 'B', 'C', 'D']
        wx.StaticText(panel, -1, "Select option:", (15, 20))
        self.myChoice=wx.Choice(panel, -1, (85, 18), choices=sampleList)
        self.myChoice.Bind(wx.EVT_SET_FOCUS,self.onFocus)
        # options available in drop down list: A,B,C,D

    def onFocus(self,evt):
        # refresh your items with .Clear, .Append(), etc
        # I'm just adding new item every time user clicks on control              
        self.myChoice.Append('''I'm new here''')

app = wx.PySimpleApp()
myFrame().Show()
app.MainLoop()

etc.

Personally, I'd avoid refreshing list everytime user clicks on Choice widget; that creates unnecessary queries. Instead, I'd rather re-fill choices after clicking e.g. refresh button next to your choices widget, or - if you can open your choices modification frame as a dialog - after closing data modification dialog.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top