def set_default_buttons_img(self):
"""
Set random ComboBox coins
"""
nb1, nb2 = None, None
while True:
nb1 = randint(0, len(self.coin_list)-1)
nb2 = randint(0, len(self.coin_list)-1)
if nb1 != nb2:
# DEBUG
#print(nb1, nb2, len(self.list_imgs_path))
break
self.combo.set_active(nb1)
self.combo2.set_active(nb2)
self.img_name1, self.img_name2 = self.coin_list[nb1], self.coin_list[nb2]
python类ComboBox()的实例源码
def on_stt_combobox_changed(self, combo: Gtk.ComboBox):
selection = combo.get_active()
if selection == 0:
config['default_stt'] = 'google'
elif selection == 1:
credential_dialog = WatsonCredentialsDialog(
self.config_window.window)
response = credential_dialog.run()
if response == Gtk.ResponseType.OK:
username = credential_dialog.username_field.get_text()
password = credential_dialog.password_field.get_text()
config['default_stt'] = 'watson'
config['watson_stt_config']['username'] = username
config['watson_stt_config']['password'] = password
else:
self.config_window.init_stt_combobox()
credential_dialog.destroy()
elif selection == 2:
credential_dialog = BingCredentialDialog(
self.config_window.window)
response = credential_dialog.run()
if response == Gtk.ResponseType.OK:
api_key = credential_dialog.api_key_field.get_text()
config['default_stt'] = 'bing'
config['bing_speech_api_key']['username'] = api_key
else:
self.config_window.init_stt_combobox()
credential_dialog.destroy()
def get_combobox(self):
"""
@description: get the combobox of the toolbar
@return: a Gtk.Combobox
"""
# the data in the model, of type string
listmodel = Gtk.ListStore(str)
# append the data in the model
listmodel.append(['All'])
listmodel.append(['External'])
listmodel.append(['Internal'])
listmodel.append(['Modules'])
# a combobox to see the data stored in the model
combobox = Gtk.ComboBox(model=listmodel)
combobox.set_tooltip_text("What type of command to add?")
# a cellrenderer to render the text
cell = Gtk.CellRendererText()
# pack the cell into the beginning of the combobox, allocating
# no more space than needed
combobox.pack_start(cell, False)
# associate a property ("text") of the cellrenderer (cell) to a column (column 0)
# in the model used by the combobox
combobox.add_attribute(cell, "text", 0)
# the first row is the active one by default at the beginning
combobox.set_active(0)
# connect the signal emitted when a row is selected to the callback function
combobox.connect("changed", self.on_combochanged)
return combobox
# callback function attach to the combobox
def __get_combobox(self, store, iter):
"""
@description: get the combobox of the toolbar
@return: a Gtk.Combobox
"""
# the data in the model, of type string
listmodel = Gtk.ListStore(str)
# append the data in the model
self.dic = {}
self.dic[0] = 'time'
listmodel.append(['time'])
self.dic[1] = 'power'
listmodel.append(['power'])
self.dic[2] = 'clipboard'
listmodel.append(['clipboard'])
selected = 0
if iter is not None:
for i in range(len(self.dic)):
if self.dic[i] == store[iter][1]:
selected = i
# a combobox to see the data stored in the model
combobox = Gtk.ComboBox(model=listmodel)
combobox.set_tooltip_text("Which internal command to choose" + '?')
# a cellrenderer to render the text
cell = Gtk.CellRendererText()
# pack the cell into the beginning of the combobox, allocating
# no more space than needed
combobox.pack_start(cell, False)
# associate a property ("text") of the cellrenderer (cell) to a column (column 0)
# in the model used by the combobox
combobox.add_attribute(cell, "text", 0)
# the first row is the active one by default at the beginning
combobox.set_active(selected)
return combobox
def __get_combobox(self):
"""
@description: get the combobox of the toolbar
@return: a Gtk.Combobox
"""
# the data in the model, of type string
listmodel = Gtk.ListStore(str)
# append the data in the model
selected = 4
i = 0
self.language_list = ['en-AU', 'en-CA', 'en-GB', 'en-IN', 'en-US']
for language_name in self.language_list:
listmodel.append([language_name])
if language_name == self.locale:
selected = i
i += 1
# a combobox to see the data stored in the model
combobox = Gtk.ComboBox(model=listmodel)
combobox.set_tooltip_text("What format to choose?")
# a cellrenderer to render the text
cell = Gtk.CellRendererText()
# pack the cell into the beginning of the combobox, allocating
# no more space than needed
combobox.pack_start(cell, False)
# associate a property ("text") of the cellrenderer (cell) to a column (column 0)
# in the model used by the combobox
combobox.add_attribute(cell, "text", 0)
# the first row is the active one by default at the beginning
combobox.set_active(selected)
# connect the signal emitted when a row is selected to the callback function
combobox.connect("changed", self.on_combochanged)
return combobox
def __get_rate_combobox(self):
"""
@description: get the sample rate combobox of the toolbar
@return: a Gtk.Combobox
"""
# the data in the model, of type string
listmodel = Gtk.ListStore(int)
# append the data in the model
selected = 0
i = 0
self.rate_list = [8000, 16000]
for rate_name in self.rate_list:
listmodel.append([rate_name])
if rate_name == self.audio_rate:
selected = i
i += 1
# a combobox to see the data stored in the model
rate_combobox = Gtk.ComboBox(model=listmodel)
rate_combobox.set_tooltip_text("Set Sampling Rate")
# a cellrenderer to render the text
rate_cell = Gtk.CellRendererText()
# pack the cell into the beginning of the combobox, allocating
# no more space than needed
rate_combobox.pack_start(rate_cell, False)
# associate a property ("text") of the cellrenderer (cell) to a column (column 0)
# in the model used by the combobox
rate_combobox.add_attribute(rate_cell, "text", 0)
# the first row is the active one by default at the beginning
rate_combobox.set_active(selected)
# connect the signal emitted when a row is selected to the callback function
rate_combobox.connect("changed", self.on_rate_combochanged)
return rate_combobox
def __get_speaker_combobox(self):
"""
@description: get the speaker combobox of the toolbar
@return: a Gtk.Combobox
"""
# the data in the model, of type string
listmodel = Gtk.ListStore(str)
# append the data in the model
selected = 0
i = 0
self.speaker_list = ['Mama (Male)', 'His Wife (Female)']
for speaker_name in self.speaker_list:
listmodel.append([speaker_name])
if speaker_name == self.speaker:
selected = i
i += 1
# a combobox to see the data stored in the model
speaker_combobox = Gtk.ComboBox(model=listmodel)
speaker_combobox.set_tooltip_text("Whose speaker to choose?")
# a cellrenderer to render the text
speaker_cell = Gtk.CellRendererText()
# pack the cell into the beginning of the combobox, allocating
# no more space than needed
speaker_combobox.pack_start(speaker_cell, False)
# associate a property ("text") of the cellrenderer (cell) to a column (column 0)
# in the model used by the combobox
speaker_combobox.add_attribute(speaker_cell, "text", 0)
# the first row is the active one by default at the beginning
speaker_combobox.set_active(selected)
# connect the signal emitted when a row is selected to the callback function
speaker_combobox.connect("changed", self.on_speaker_combochanged)
return speaker_combobox
def get_new_values(self, widget):
"""
When ComboBox changed, main window will update all labels informations
"""
nb1, nb2 = self.combo.get_active(), self.combo2.get_active()
self.img_name1, self.img_name2 = self.coin_list[nb1], self.coin_list[nb2]
self.get_items(widget = "window")
def sComboBox(exname, name, strings):
liststore = Gtk.ListStore(str)
for s in strings:
liststore.append([s])
g = Gtk.ComboBox.new_with_model_and_entry(liststore)
g.set_entry_text_column(0)
g.get_child().set_text(cfg.get_string("%s/%s" % (exname, name)))
g.connect('changed', lambda w:
cfg.set_string("%s/%s" % (exname, name),
w.get_child().get_text().decode("utf-8")))
return g
def __init__(self, keyfile, settings, key, options, valtype="string"):
self.key = key
self.keyfile = keyfile
try:
self.value = keyfile.get_string(GROUP_NAME, key)
except:
self.value = settings.get_string(key)
Gtk.ComboBox.__init__(self)
renderer_text = Gtk.CellRendererText()
self.pack_start(renderer_text, True)
self.add_attribute(renderer_text, "text", 1)
self.set_valign(Gtk.Align.CENTER)
# assume all keys are the same type (mixing types is going to cause an error somewhere)
var_type = type(options[0][0])
self.model = Gtk.ListStore(var_type, str)
self.valtype = valtype
self.option_map = {}
for option in options:
self.option_map[option[0]] = self.model.append([option[0], option[1]])
self.set_model(self.model)
self.set_id_column(0)
if self.value in self.option_map.keys():
self.set_active_iter(self.option_map[self.value])
self.connect("changed", self.on_changed)