当前位置: 首页 > 文档资料 > PyGTK 教程 >

06 PyGTK 中的部件

优质
小牛编辑
121浏览
2023-12-01

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

部件是GUI应用程序基本的构建单元。经过这些年的发展,有些部件已经成为所有的操作系统平台上的所有工具包的一个标准。例如:按钮,复选框或者滚动条。PyGTK工具包的设计哲学是在一个最低的水平保持部件的数量。而比较特殊的部件都将作为一个自定义部件被创建。

Label

标签(Label)部件就是显示数量有限的只读的文本。

Code:label.py

#!/usr/bin/python

# ZetCode PyGTK tutorial 
#
# This example demonstrates the Label widget
#
# author: jan bodnar
# website: zetcode.com 
# last edited: February 2009

import gtk

lyrics = """Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
and sniff me out like I was Tanqueray

cause you're my fella, my guy
hand me your stella and fly
by the time I'm out the door
you tear men down like Roger Moore

I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good"""

class PyApp(gtk.Window): 
    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_border_width(8)
        self.connect("destroy", gtk.main_quit)
        self.set_title("You know I'm no Good")
        
        label = gtk.Label(lyrics)
        self.add(label)
        self.show_all()

PyApp()
gtk.main()

上面的代码示例在窗口上展示了一些歌词。

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

这是我们要显示的文本。

self.set_border_width(8)

这个标签被一些空白空间包围着。

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

这个标签被创建,并且被添加到窗口中。

Figure:Label widget

CheckButton

复选按钮(也叫复选框)是一种有两个状态的部件,开和关。打开状态是被一个复选标记显示出来。它一般用来指示一些布尔属性。

Code:checkbutton.py

#!/usr/bin/python

# ZetCode PyGTK tutorial 
#
# This example demonstrates the CheckButton widget
#
# author: jan bodnar
# website: zetcode.com 
# last edited: February 2009

import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()
        self.set_title("Check Button")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(250, 200)

        fixed = gtk.Fixed()
        button = gtk.CheckButton("Show title")
        button.set_active(True)
        button.unset_flags(gtk.CAN_FOCUS)
        button.connect("clicked", self.on_clicked)

        fixed.put(button, 50, 50)

        self.connect("destroy", gtk.main_quit)
        self.add(fixed)
        self.show_all()

    def on_clicked(self, widget):
        if widget.get_active():
            self.set_title("Check Button")
        else:
           self.set_title("")
        
PyApp()
gtk.main()

我们将会依据复选按钮的状态在窗口的标题栏上显示一个标题。

button = gtk.CheckButton("Show title")

复选按钮部件被创建。

button.set_active(True)

标题在默认状态下是可见的,因此默认选中复选按钮。

if widget.get_active():
    self.set_title("Check Button")
else:
    self.set_title("")

如果复选按钮是选中的,我们就显示标题,否则我们将标题栏上的标题设置为空的。

Figure:Check button

ComboBox

组合框是一种允许用户从一个选项列表中做选择的部件。

Code:combobox.py

#!/usr/bin/python

# ZetCode PyGTK tutorial 
#
# This example demonstrates the ComboBox widget
#
# author: jan bodnar
# website: zetcode.com 
# last edited: February 2009

import gtk

class PyApp(gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_title("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('Ubuntu')
        cb.append_text('Mandriva')
        cb.append_text('Redhat')
        cb.append_text('Gentoo')
        cb.append_text('Mint')
        
        fixed = gtk.Fixed()
        fixed.put(cb, 50, 30)
        self.label = gtk.Label("-")
        fixed.put(self.label, 50, 140)
        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()) 

PyApp()
gtk.main()

这个例子展示了一个组合框和一个标签。组合框有一个六个选项的列表。这些都是Linux发行版的名字。标签展示从组合框中被选中的选项。

cb = gtk.combo_box_new_text()

这个gtk.combo_box_new_text()函数是一个便利函数,它构造了一个新的文本组合框。它是一个仅仅显示字符串的ComboBox

cb.append_text('Ubuntu')
cb.append_text('Mandriva')
cb.append_text('Redhat')
cb.append_text('Gentoo')
cb.append_text('Mint')

这个复选框被填充了文本的数据。

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

在on_changed()方法中,我们从组合框得到选中的文本,并且将它设置到标签中。

Figure:ComboBox

Image

下面的例子是介绍图像部件。这个部件是用来显示图片的。

Code:image.py

#!/usr/bin/python

# ZetCode PyGTK tutorial 
#
# This example demonstrates the Image widget
#
# author: jan bodnar
# website: zetcode.com 
# last edited: February 2009

import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_title("Red Rock")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_border_width(2)

        image = gtk.Image()
        image.set_from_file("redrock.png")

        self.connect("destroy", gtk.main_quit)
        self.add(image)
        self.show_all()

PyApp()
gtk.main()

我们在窗口中展示了Red Rock城堡。

image = gtk.Image()

Image部件被创建。

image.set_from_file("redrock.png")

我们为Image部件设置了一个png格式的图片。这个图片是从磁盘上的文件中加载的。

Figure:Image

在这个章节中,我们展示了PyGTK编程库的基本部件的第一包。