PyGTK - Hello World



使用 PyGTK 创建窗口非常简单。首先,我们需要在代码中导入 gtk 模块。

import gtk

gtk 模块包含 gtk.Window 类。它的对象构建一个顶级窗口。我们从 gtk.Window 派生一个类。

class PyApp(gtk.Window):

定义构造函数并调用 gtk.window 类的 show_all() 方法。

def __init__(self):
   super(PyApp, self).__init__()
   self.show_all()

现在,我们必须声明此类的对象并通过调用其 main() 方法启动事件循环。

PyApp()
gtk.main()

建议我们在父窗口中添加一个标签 “Hello World”

label = gtk.Label("Hello World")
self.add(label)

以下是显示 “Hello World” 的完整代码:

import gtk

class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
      self.set_default_size(300,200)
      self.set_title("Hello World in PyGTK")
      label = gtk.Label("Hello World")
      self.add(label)
      self.show_all()
PyApp()
gtk.main()

上述代码的实现将产生以下输出:

Hello World PyGTK
广告

© . All rights reserved.