免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2945 | 回复: 0
打印 上一主题 下一主题

PyGTK - 2.1. Hello World in PyGTK [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-15 22:10 |只看该作者 |倒序浏览

                                                    1   #!/usr/bin/env python
    2
    3   # example helloworld.py
    4
    5   import pygtk
    6   pygtk.require('2.0')
    7   import gtk
    8
    9   class HelloWorld:
   10
   11       # This is a callback function. The data arguments are ignored
   12       # in this example. More on callbacks below.
   13       def hello(self, widget, data=None):
   14           print "Hello World"
   15
   16       def delete_event(self, widget, event, data=None):
   17           # If you return FALSE in the "delete_event" signal handler,
   18           # GTK will emit the "destroy" signal. Returning TRUE means
   19           # you don't want the window to be destroyed.
   20           # This is useful for popping up 'are you sure you want to quit?'
   21           # type dialogs.
   22           print "delete event occurred"
   23
   24           # Change FALSE to TRUE and the main window will not be destroyed
   25           # with a "delete_event".
   26           return False
   27
   28       # Another callback
   29       def destroy(self, widget, data=None):
   30           gtk.main_quit()
   31
   32       def __init__(self):
   33           # create a new window
   34           self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   35
   36           # When the window is given the "delete_event" signal (this is given
   37           # by the window manager, usually by the "close" option, or on the
   38           # titlebar), we ask it to call the delete_event () function
   39           # as defined above. The data passed to the callback
   40           # function is NULL and is ignored in the callback function.
   41           self.window.connect("delete_event", self.delete_event)
   42
   43           # Here we connect the "destroy" event to a signal handler.
   44           # This event occurs when we call gtk_widget_destroy() on the window,
   45           # or if we return FALSE in the "delete_event" callback.
   46           self.window.connect("destroy", self.destroy)
   47
   48           # Sets the border width of the window.
   49           self.window.set_border_width(10)
   50
   51           # Creates a new button with the label "Hello World".
   52           self.button = gtk.Button("Hello World")
   53
   54           # When the button receives the "clicked" signal, it will call the
   55           # function hello() passing it None as its argument.  The hello()
   56           # function is defined above.
   57           self.button.connect("clicked", self.hello, None)
   58
   59           # This will cause the window to be destroyed by calling
   60           # gtk_widget_destroy(window) when "clicked".  Again, the destroy
   61           # signal could come from here, or the window manager.
   62           self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
   63
   64           # This packs the button into the window (a GTK container).
   65           self.window.add(self.button)
   66
   67           # The final step is to display this newly created widget.
   68           self.button.show()
   69
   70           # and the window
   71           self.window.show()
   72
   73       def main(self):
   74           # All PyGTK applications must have a gtk.main(). Control ends here
   75           # and waits for an event to occur (like a key press or mouse event).
   76           gtk.main()
   77
   78   # If the program is run directly or passed as an argument to the python
   79   # interpreter then create a HelloWorld instance and show it
   80   if __name__ == "__main__":
   81       hello = HelloWorld()
   82       hello.main()

最经典的Hello World程序。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105486/showart_2095130.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP