原文: http://zetcode.com/gui/pygtk/widgets/

在 PyGTK 编程教程的这一部分中,我们将介绍一些 PyGTK 小部件。

小部件是 GUI 应用的基本构建块。 多年来,几个小部件已成为所有 OS 平台上所有工具包中的标准。 例如,按钮,复选框或滚动条。 PyGTK 工具箱的理念是将小部件的数量保持在最低水平。 将创建更多专门的小部件作为自定义 PyGTK 小部件。

Label

Label小部件显示有限数量的只读文本。

label.py

  1. #!/usr/bin/python
  2. # ZetCode PyGTK tutorial
  3. #
  4. # This example demonstrates the Label widget
  5. #
  6. # author: jan bodnar
  7. # website: zetcode.com
  8. # last edited: February 2009
  9. import gtk
  10. lyrics = """Meet you downstairs in the bar and heard
  11. your rolled up sleeves and your skull t-shirt
  12. You say why did you do it with him today?
  13. and sniff me out like I was Tanqueray
  14. cause you're my fella, my guy
  15. hand me your stella and fly
  16. by the time I'm out the door
  17. you tear men down like Roger Moore
  18. I cheated myself
  19. like I knew I would
  20. I told ya, I was trouble
  21. you know that I'm no good"""
  22. class PyApp(gtk.Window):
  23. def __init__(self):
  24. super(PyApp, self).__init__()
  25. self.set_position(gtk.WIN_POS_CENTER)
  26. self.set_border_width(8)
  27. self.connect("destroy", gtk.main_quit)
  28. self.set_title("You know I'm no Good")
  29. label = gtk.Label(lyrics)
  30. self.add(label)
  31. self.show_all()
  32. PyApp()
  33. gtk.main()

该代码示例在窗口上显示了一些歌词。

  1. lyrics = """Meet you downstairs in the bar and heard
  2. your rolled up sleeves and your skull t-shirt
  3. ..."""

这是我们显示的文本。

  1. self.set_border_width(8)

Label周围有一些空白。

  1. label = gtk.Label(lyrics)
  2. self.add(label)

Label小部件已创建并添加到窗口。

PyGTK 中的小部件 - 图1

图:Label小部件

CheckButton

CheckButton是具有两种状态的窗口小部件:打开和关闭。 n 状态通过复选标记显示。 它用来表示一些布尔属性。

checkbutton.py

  1. #!/usr/bin/python
  2. # ZetCode PyGTK tutorial
  3. #
  4. # This example demonstrates the CheckButton widget
  5. #
  6. # author: jan bodnar
  7. # website: zetcode.com
  8. # last edited: February 2009
  9. import gtk
  10. class PyApp(gtk.Window):
  11. def __init__(self):
  12. super(PyApp, self).__init__()
  13. self.set_title("Check Button")
  14. self.set_position(gtk.WIN_POS_CENTER)
  15. self.set_default_size(250, 200)
  16. fixed = gtk.Fixed()
  17. button = gtk.CheckButton("Show title")
  18. button.set_active(True)
  19. button.unset_flags(gtk.CAN_FOCUS)
  20. button.connect("clicked", self.on_clicked)
  21. fixed.put(button, 50, 50)
  22. self.connect("destroy", gtk.main_quit)
  23. self.add(fixed)
  24. self.show_all()
  25. def on_clicked(self, widget):
  26. if widget.get_active():
  27. self.set_title("Check Button")
  28. else:
  29. self.set_title("")
  30. PyApp()
  31. gtk.main()

根据CheckButton的状态,我们将在窗口的标题栏中显示标题。

  1. button = gtk.CheckButton("Show title")

CheckButton小部件已创建。

  1. button.set_active(True)

默认情况下标题是可见的,因此我们默认情况下选中复选按钮。

  1. if widget.get_active():
  2. self.set_title("Check Button")
  3. else:
  4. self.set_title("")

如果选中CheckButton,我们将显示标题。 否则,我们将在标题栏中放置空白文本。

PyGTK 中的小部件 - 图2

图:CheckButton

ComboBox

ComboBox是一个小部件,允许用户从选项列表中进行选择。

combobox.py

  1. #!/usr/bin/python
  2. # ZetCode PyGTK tutorial
  3. #
  4. # This example demonstrates the ComboBox widget
  5. #
  6. # author: jan bodnar
  7. # website: zetcode.com
  8. # last edited: February 2009
  9. import gtk
  10. class PyApp(gtk.Window):
  11. def __init__(self):
  12. super(PyApp, self).__init__()
  13. self.set_title("ComboBox")
  14. self.set_default_size(250, 200)
  15. self.set_position(gtk.WIN_POS_CENTER)
  16. cb = gtk.combo_box_new_text()
  17. cb.connect("changed", self.on_changed)
  18. cb.append_text('Ubuntu')
  19. cb.append_text('Mandriva')
  20. cb.append_text('Redhat')
  21. cb.append_text('Gentoo')
  22. cb.append_text('Mint')
  23. fixed = gtk.Fixed()
  24. fixed.put(cb, 50, 30)
  25. self.label = gtk.Label("-")
  26. fixed.put(self.label, 50, 140)
  27. self.add(fixed)
  28. self.connect("destroy", gtk.main_quit)
  29. self.show_all()
  30. def on_changed(self, widget):
  31. self.label.set_label(widget.get_active_text())
  32. PyApp()
  33. gtk.main()

该示例显示了一个组合框和一个标签。 组合框具有六个选项的列表。 这些是 Linux 发行版的名称。 标签窗口小部件显示了从组合框中选择的选项。

  1. cb = gtk.combo_box_new_text()

gtk.combo_box_new_text()函数是一种便捷函数,可构造一个新的文本组合框。 它是只显示字符串的ComboBox

  1. cb.append_text('Ubuntu')
  2. cb.append_text('Mandriva')
  3. cb.append_text('Redhat')
  4. cb.append_text('Gentoo')
  5. cb.append_text('Mint')

ComboBox充满了文本数据。

  1. self.label.set_label(widget.get_active_text())

on_changed()方法内部,我们从组合框中获取选定的文本并将其设置为标签。

PyGTK 中的小部件 - 图3

图:ComboBox

Image

下一个示例介绍Image小部件。 此小部件显示图片。

image.py

  1. #!/usr/bin/python
  2. # ZetCode PyGTK tutorial
  3. #
  4. # This example demonstrates the Image widget
  5. #
  6. # author: jan bodnar
  7. # website: zetcode.com
  8. # last edited: February 2009
  9. import gtk
  10. class PyApp(gtk.Window):
  11. def __init__(self):
  12. super(PyApp, self).__init__()
  13. self.set_title("Red Rock")
  14. self.set_position(gtk.WIN_POS_CENTER)
  15. self.set_border_width(2)
  16. image = gtk.Image()
  17. image.set_from_file("redrock.png")
  18. self.connect("destroy", gtk.main_quit)
  19. self.add(image)
  20. self.show_all()
  21. PyApp()
  22. gtk.main()

我们在窗口中显示红色岩石城堡。

  1. image = gtk.Image()

Image小部件已创建。

  1. image.set_from_file("redrock.png")

我们将 PNG 图像设置为Image小部件。 图片是从磁盘上的文件加载的。

PyGTK 中的小部件 - 图4

图:图像

在本章中,我们展示了 PyGTK 编程库的第一组基本小部件。