📜  PyGTK-ComboBox类

📅  最后修改于: 2020-11-08 07:50:55             🧑  作者: Mango


ComboBox是任何GUI工具包中功能强大且流行的小部件。它提供了用户可以选择的项目下拉列表。 gtk.ComboBox小部件实现了CellLayout接口,并提供了许多方法来管理项目的显示。

gtk.ComboBox类的对象与ListSore相关联,ListSore是一个列表模型,可以与显示项目集合的小部件一起使用。使用append()方法将项目添加到ListStore。此外,将创建一个CellRendererText对象并将其打包到组合框中。

请按照以下步骤设置组合框。

combobox = gtk.ComboBox()
store = gtk.ListStore(gobject.TYPE_STRING)
cell = gtk.CellRendererText()
combobox.pack_start(cell)
combobox.add_attribute(cell, 'text', 0)

PyGTK提供了一种便捷方法-gtk.combo_box_new_text()来创建一个组合框,而不是使用列表存储。关联的便捷方法append_text(),prepend_text(),insert_text()和remove_text()用于管理组合框内容。

gtk.ComboBox类具有以下方法-

S.NO Methods and Description
1

set_wrap_width()

Sets the number of columns to be displayed in the popup table layout

2

get_active()

Returns the value of the “active” property which is the index in the model of the currently active item

3

set_active()

Sets the active item of the combo_box to the item with the model index specified

4

set_model()

Sets the model used by the combo box

5

append_text()

Appends the string specified by text to the list of strings stored in the combo box list store

6

Insert_text()

Inserts the string specified by text in the combo box gtk.ListStore at the index specified by position

7

prepend_text()

Prepends the string specified by text to the list of strings stored in the list store

8

remove_text()

Removes the string at the index specified by position in the associated liststore

9

get_active_text()

Returns the currently active string

ComboBox小部件发出以下信号-

changed This is emitted when a new item in the combo box is selected
move_active This is a keybinding signal which gets emitted to move the active selection.
Popdown This is a keybinding signal which gets emitted to popdown the combo box list. The default bindings for this signal are Alt+Up and Escape
Popup This is a keybinding signal which gets emitted to popup the combo box list. The default bindings for this signal are Alt+Down.

下面给出了用于演示ComboBox的两个示例代码。

例子1

在此示例中,将使用流行的Python GUI工具箱的名称填充ListStore,并将其与ComboBox小部件关联。当用户做出选择时,发出改变的信号。它连接到回调函数以显示用户的选择。

import pygtk
pygtk.require('2.0')
import gtk

class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("ComboBox with ListStore")
      self.set_default_size(250, 200)
      self.set_position(gtk.WIN_POS_CENTER)
      
      combobox = gtk.ComboBox()
      store = gtk.ListStore(str)
      cell = gtk.CellRendererText()
      combobox.pack_start(cell)
      combobox.add_attribute(cell, 'text', 0)
      fixed = gtk.Fixed()
      lbl = gtk.Label("select a GUI toolkit")
      fixed.put(lbl, 25,75)
      fixed.put(combobox, 125,75)
      lbl2 = gtk.Label("Your choice is:")
      fixed.put(lbl2, 25,125)
      self.label = gtk.Label("")
      fixed.put(self.label, 125,125)
      self.add(fixed)
      
      store.append (["PyQt"])
      store.append (["Tkinter"])
      store.append (["WxPython"])
      store.append (["PyGTK"])
      store.append (["PySide"])
      combobox.set_model(store)
      combobox.connect('changed', self.on_changed)
      combobox.set_active(0)
      self.connect("destroy", gtk.main_quit)
      self.show_all()
      return
   
   def on_changed(self, widget):
      self.label.set_label(widget.get_active_text())
      return
      
if __name__ == '__main__':
PyApp()
gtk.main()

执行后,程序显示以下输出-

组合框

例子2

该程序的第二个版本使用便捷方法combo_box_new_text()创建一个组合框,并使用append_text()函数在其中添加字符串。在这两个程序中, get_active_text()方法用于获取用户的选择并显示在窗口的标签上。

import gtk

class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("Simple ComboBox")
      self.set_default_size(250, 200)
      self.set_position(gtk.WIN_POS_CENTER)
      
      cb = gtk.combo_box_new_text()
      cb.connect("changed", self.on_changed)
      cb.append_text('PyQt')
      cb.append_text('Tkinter')
      cb.append_text('WxPython')
      cb.append_text('PyGTK')
      cb.append_text('PySide')
      
      fixed = gtk.Fixed()
      lbl = gtk.Label("select a GUI toolkit")
      fixed.put(lbl, 25,75)
      
      fixed.put(cb, 125,75)
      lbl2 = gtk.Label("Your choice is:")
      fixed.put(lbl2, 25,125)
      
      self.label = gtk.Label("")
      fixed.put(self.label, 125,125)
      self.add(fixed)
      self.connect("destroy", gtk.main_quit)
      self.show_all()
   
   def on_changed(self, widget):
      self.label.set_label(widget.get_active_text())
if __name__ == '__main__':
   PyApp()
   gtk.main()

该程序的输出类似于上一个程序的输出。

组合框