So I'm trying to make a relatively simple TTS program using the speech module. The place I'm getting stuck at is: I want to make a list of saved text available through a combobox in the window, and if you add or delete any presets from it, it won't update the choices until the program is reloaded. Is there a way to update the choices in real time?
The combobox initializes like this:
#I have a previously set txt file with a list of presets, and its partial destination saved in a variable "savefile"
fh = open(savefile + '//presets.txt')
presets = fh.read().split('\n')
self.presetList = presets
#self.presetList is now the list of choices for the combobox, causing it to update upon loading the program
self.presetbox = wx.ComboBox(self, pos=(90, 100), size=(293, -1), choices=self.presetList, style=wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.presetbox)
and later on, say, to clear all choices, I would need something like this:
self.emptyList = []
self.presetbox.SetChoices(self.emptyList)
Is there a way to do this? If so that'd be great! :)
ComboBox is a mix of TextCtrl and ListBox ( http://wxpython-users.1045709.n5.nabble.com/Question-on-wx-ComboBox-Clear-td2353059.html )
To reset the field, you can use self.presetbox.SetValue('')
(in case your ComboBox is not READONLY)
I'm referencing the C++ WxWidgets documentation instead of the wxPython docs, because they are usually better :), but the names should be the same.
wxComboBox
derives from wxItemContainer
, which has a series of functions simply called Set
which you can pass in a list of strings to set the combo box contents. There are also Append
and Insert
functions you can use, as well as a Clear
function that clears all the choices.
wxItemContainer
docs are here: http://docs.wxwidgets.org/trunk/classwx_item_container.html
This worked for me, binding the EVT_COMBOBOX_DROPDOWN to update the list, than bind EVT_COMBOBOX to run a function on user's choice. That way updates, then shows the contents updated. If new choices are less, it doesn't leave the white space of the previous ones. Also you can set the combobox as READONLY to behave as a choice widget (not editable by user) as below. This code was tested under Windows, Python 3.6 and the newer wxpython (phoenix).
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (300,200))
panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(panel,label = "Your choice:" ,style = wx.ALIGN_CENTRE)
box.Add(self.label, 0 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20)
cblbl = wx.StaticText(panel,label = "Combo box",style = wx.ALIGN_CENTRE)
box.Add(cblbl,0,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
# initial chioces are 5
#------------------------------------------------------
languages = ['C', 'C++', 'Python', 'Java', 'Perl']
#------------------------------------------------------
self.combo = wx.ComboBox(panel,choices = languages, style = wx.CB_READONLY)
box.Add(self.combo,0,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5)
self.combo.Bind(wx.EVT_COMBOBOX ,self.OnCombo)
self.combo.Bind(wx.EVT_COMBOBOX_DROPDOWN,self.updatelist)
panel.SetSizer(box)
self.Centre()
self.Show()
def OnCombo(self, event):
self.label.SetLabel("You selected "+self.combo.GetValue()+" from Combobox")
def updatelist(self, event):
# Chioces are now just 2
#------------------------------------------------------
OtrosLanguajes = ['C++', 'Python']
#------------------------------------------------------
self.combo.SetItems(OtrosLanguajes)
app = wx.App()
Mywin(None, 'ComboBox and Choice demo')
app.MainLoop()
The parent class of wx.ComboBox
is ItemContainer
.
It has the method Clear()
.
Use the Clear()
method on the wx.ComboBox
object to clear all data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.