目录

PyGTK - Hello World( Hello World)

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

import gtk

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

class PyApp(gtk.Window):

定义构造函数并调用gtk.window类的show_all 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
↑回到顶部↑
WIKI教程 @2018