特殊:Badtitle/NS100:PythonRecipes/HelloWorld:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
(未显示同一用户的4个中间版本) | |||
第1行: | 第1行: | ||
{{From|https://help.ubuntu.com/community/PythonRecipes/HelloWorld}} | {{From|https://help.ubuntu.com/community/PythonRecipes/HelloWorld}} | ||
{{Languages|UbuntuHelp:PythonRecipes/HelloWorld}} | {{Languages|UbuntuHelp:PythonRecipes/HelloWorld}} | ||
<<Include(Tag/StyleCleanup)>> | |||
<<Include(Tag/ContentCleanup)>> | |||
<<Include(Tag/NeedsExpansion)>> | |||
#title Hello World! | #title Hello World! | ||
Parent: (unknown) [[UbuntuHelp:PythonRecipes/HelloWorld/PageDiscussion|Discuss this page]] | |||
''This Python Recipe is currently being written'' | ''This Python Recipe is currently being written'' | ||
Here is a simple GTK Example for a Hello World Program written using the Programming language called Python. Most of the code below is commented, please feel free to edit the code to match any standards. To learn more about Python, see [http://python.org Python.org] or [[UbuntuHelp:PythonRecipes|Python Recipes]]. | Here is a simple GTK Example for a Hello World Program written using the Programming language called Python. Most of the code below is commented, please feel free to edit the code to match any standards. To learn more about Python, see [http://python.org Python.org] or [[UbuntuHelp:PythonRecipes|Python Recipes]]. | ||
=== Screenshot === | === Screenshot === | ||
https://help.ubuntu.com/community/ | https://help.ubuntu.com/community/PythonRecipes/HelloWorld?action=AttachFile&do=get&target=hello-world.png | ||
=== Code === | === Code === | ||
'''Note:''' ''The code may look a little long, but 75% of it is just commented.'' | '''Note:''' ''The code may look a little long, but 75% of it is just commented.'' | ||
<pre><nowiki>#!python | <pre><nowiki>#!python | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# example helloworld.py | # example helloworld.py | ||
import pygtk | import pygtk | ||
pygtk.require('2.0') | pygtk.require('2.0') | ||
import gtk | import gtk | ||
class HelloWorld: | class HelloWorld: | ||
# This is a callback function. The data arguments are ignored | |||
# in this example. More on callbacks below. | # This is a callback function. The data arguments are ignored | ||
def hello(self, widget, data=None): | # in this example. More on callbacks below. | ||
print "Hello World" | def hello(self, widget, data=None): | ||
def delete_event(self, widget, event, data=None): | print "Hello World" | ||
# If you return FALSE in the "delete_event" signal handler, | |||
# GTK will emit the "destroy" signal. Returning TRUE means | def delete_event(self, widget, event, data=None): | ||
# you don't want the window to be destroyed. | # If you return FALSE in the "delete_event" signal handler, | ||
# This is useful for popping up 'are you sure you want to quit?' | # GTK will emit the "destroy" signal. Returning TRUE means | ||
# type dialogs. | # you don't want the window to be destroyed. | ||
print "delete event occurred" | # This is useful for popping up 'are you sure you want to quit?' | ||
# Change FALSE to TRUE and the main window will not be destroyed | # type dialogs. | ||
# with a "delete_event". | print "delete event occurred" | ||
return False | |||
def destroy(self, widget, data=None): | # Change FALSE to TRUE and the main window will not be destroyed | ||
print "destroy signal occurred" | # with a "delete_event". | ||
gtk.main_quit() | return False | ||
def | |||
# create a new window | def destroy(self, widget, data=None): | ||
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | print "destroy signal occurred" | ||
# When the window is given the "delete_event" signal (this is given | gtk.main_quit() | ||
# by the window manager, usually by the "close" option, or on the | |||
# titlebar), we ask it to call the delete_event () function | def __init__(self): | ||
# as defined above. The data passed to the callback | # create a new window | ||
# function is NULL and is ignored in the callback function. | self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | ||
self.window.connect("delete_event", self.delete_event) | |||
# Here we connect the "destroy" event to a signal handler. | # When the window is given the "delete_event" signal (this is given | ||
# This event occurs when we call gtk_widget_destroy() on the window, | # by the window manager, usually by the "close" option, or on the | ||
# or if we return FALSE in the "delete_event" callback. | # titlebar), we ask it to call the delete_event () function | ||
self.window.connect("destroy", self.destroy) | # as defined above. The data passed to the callback | ||
# Sets the border width of the window. | # function is NULL and is ignored in the callback function. | ||
self.window.set_border_width(10) | self.window.connect("delete_event", self.delete_event) | ||
# Creates a new button with the label "Hello World". | |||
self.button = gtk.Button("Hello World") | # Here we connect the "destroy" event to a signal handler. | ||
# When the button receives the "clicked" signal, it will call the | # This event occurs when we call gtk_widget_destroy() on the window, | ||
# function hello() passing it None as its argument. The hello() | # or if we return FALSE in the "delete_event" callback. | ||
# function is defined above. | self.window.connect("destroy", self.destroy) | ||
self.button.connect("clicked", self.hello, None) | |||
# This will cause the window to be destroyed by calling | # Sets the border width of the window. | ||
# gtk_widget_destroy(window) when "clicked". Again, the destroy | self.window.set_border_width(10) | ||
# signal could come from here, or the window manager. | |||
self.button.connect_object("clicked", gtk.Widget.destroy, self.window) | # Creates a new button with the label "Hello World". | ||
# This packs the button into the window (a GTK container). | self.button = gtk.Button("Hello World") | ||
self.window.add(self.button) | |||
# The final step is to display this newly created widget. | # When the button receives the "clicked" signal, it will call the | ||
self.button.show() | # function hello() passing it None as its argument. The hello() | ||
# and the window | # function is defined above. | ||
self.window.show() | self.button.connect("clicked", self.hello, None) | ||
def main(self): | |||
# All PyGTK applications must have a gtk.main(). Control ends here | # This will cause the window to be destroyed by calling | ||
# and waits for an event to occur (like a key press or mouse event). | # gtk_widget_destroy(window) when "clicked". Again, the destroy | ||
gtk.main() | # signal could come from here, or the window manager. | ||
self.button.connect_object("clicked", gtk.Widget.destroy, self.window) | |||
# This packs the button into the window (a GTK container). | |||
self.window.add(self.button) | |||
# The final step is to display this newly created widget. | |||
self.button.show() | |||
# and the window | |||
self.window.show() | |||
def main(self): | |||
# All PyGTK applications must have a gtk.main(). Control ends here | |||
# and waits for an event to occur (like a key press or mouse event). | |||
gtk.main() | |||
# If the program is run directly or passed as an argument to the python | # If the program is run directly or passed as an argument to the python | ||
# interpreter then create a HelloWorld instance and show it | # interpreter then create a HelloWorld instance and show it | ||
if | if __name__ == "__main__": | ||
hello = HelloWorld() | hello = HelloWorld() | ||
hello.main() | hello.main() | ||
</nowiki></pre> | </nowiki></pre> | ||
=== Links === | === Links === | ||
第77行: | 第100行: | ||
*[http://python.org Python] | *[http://python.org Python] | ||
*[[UbuntuHelp:PythonRecipes|Python Recipes]] | *[[UbuntuHelp:PythonRecipes|Python Recipes]] | ||
---- | |||
[[category:CategoryProgrammingPython]] | |||
[[category:UbuntuHelp]] | [[category:UbuntuHelp]] |
2009年5月14日 (四) 17:22的最新版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/PythonRecipes/HelloWorld }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/af | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Afrikaans| [[::PythonRecipes/HelloWorld/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ar | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|العربية| [[::PythonRecipes/HelloWorld/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/az | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|azərbaycanca| [[::PythonRecipes/HelloWorld/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/bcc | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|جهلسری بلوچی| [[::PythonRecipes/HelloWorld/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/bg | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|български| [[::PythonRecipes/HelloWorld/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/br | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|brezhoneg| [[::PythonRecipes/HelloWorld/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ca | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|català| [[::PythonRecipes/HelloWorld/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/cs | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|čeština| [[::PythonRecipes/HelloWorld/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/de | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Deutsch| [[::PythonRecipes/HelloWorld/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/el | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Ελληνικά| [[::PythonRecipes/HelloWorld/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/es | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|español| [[::PythonRecipes/HelloWorld/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/fa | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|فارسی| [[::PythonRecipes/HelloWorld/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/fi | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|suomi| [[::PythonRecipes/HelloWorld/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/fr | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|français| [[::PythonRecipes/HelloWorld/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/gu | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|ગુજરાતી| [[::PythonRecipes/HelloWorld/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/he | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|עברית| [[::PythonRecipes/HelloWorld/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/hu | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|magyar| [[::PythonRecipes/HelloWorld/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/id | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Bahasa Indonesia| [[::PythonRecipes/HelloWorld/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/it | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|italiano| [[::PythonRecipes/HelloWorld/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ja | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|日本語| [[::PythonRecipes/HelloWorld/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ko | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|한국어| [[::PythonRecipes/HelloWorld/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ksh | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Ripoarisch| [[::PythonRecipes/HelloWorld/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/mr | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|मराठी| [[::PythonRecipes/HelloWorld/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ms | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Bahasa Melayu| [[::PythonRecipes/HelloWorld/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/nl | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Nederlands| [[::PythonRecipes/HelloWorld/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/no | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|norsk| [[::PythonRecipes/HelloWorld/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/oc | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|occitan| [[::PythonRecipes/HelloWorld/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/pl | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|polski| [[::PythonRecipes/HelloWorld/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/pt | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|português| [[::PythonRecipes/HelloWorld/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ro | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|română| [[::PythonRecipes/HelloWorld/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/ru | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|русский| [[::PythonRecipes/HelloWorld/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/si | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|සිංහල| [[::PythonRecipes/HelloWorld/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/sq | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|shqip| [[::PythonRecipes/HelloWorld/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/sr | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|српски / srpski| [[::PythonRecipes/HelloWorld/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/sv | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|svenska| [[::PythonRecipes/HelloWorld/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/th | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|ไทย| [[::PythonRecipes/HelloWorld/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/tr | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Türkçe| [[::PythonRecipes/HelloWorld/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/vi | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|Tiếng Việt| [[::PythonRecipes/HelloWorld/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/yue | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|粵語| [[::PythonRecipes/HelloWorld/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/zh | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|中文| [[::PythonRecipes/HelloWorld/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/zh-hans | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|中文(简体)| [[::PythonRecipes/HelloWorld/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/HelloWorld | UbuntuHelp:PythonRecipes/HelloWorld | {{#if: | :}}PythonRecipes/HelloWorld}}/zh-hant | • {{#if: UbuntuHelp:PythonRecipes/HelloWorld|中文(繁體)| [[::PythonRecipes/HelloWorld/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:PythonRecipes/HelloWorld|:PythonRecipes/HelloWorld|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :PythonRecipes/HelloWorld/zh | | {{#ifexist: PythonRecipes/HelloWorld/zh | | {{#ifeq: {{#titleparts:PythonRecipes/HelloWorld|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:PythonRecipes/HelloWorld|1|-1|}} | zh | | }}
<<Include(Tag/StyleCleanup)>> <<Include(Tag/ContentCleanup)>> <<Include(Tag/NeedsExpansion)>>
- title Hello World!
Parent: (unknown) Discuss this page This Python Recipe is currently being written Here is a simple GTK Example for a Hello World Program written using the Programming language called Python. Most of the code below is commented, please feel free to edit the code to match any standards. To learn more about Python, see Python.org or Python Recipes.
Screenshot
Code
Note: The code may look a little long, but 75% of it is just commented.
#!python #!/usr/bin/env python # example helloworld.py import pygtk pygtk.require('2.0') import gtk class HelloWorld: # This is a callback function. The data arguments are ignored # in this example. More on callbacks below. def hello(self, widget, data=None): print "Hello World" def delete_event(self, widget, event, data=None): # If you return FALSE in the "delete_event" signal handler, # GTK will emit the "destroy" signal. Returning TRUE means # you don't want the window to be destroyed. # This is useful for popping up 'are you sure you want to quit?' # type dialogs. print "delete event occurred" # Change FALSE to TRUE and the main window will not be destroyed # with a "delete_event". return False def destroy(self, widget, data=None): print "destroy signal occurred" gtk.main_quit() def __init__(self): # create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # When the window is given the "delete_event" signal (this is given # by the window manager, usually by the "close" option, or on the # titlebar), we ask it to call the delete_event () function # as defined above. The data passed to the callback # function is NULL and is ignored in the callback function. self.window.connect("delete_event", self.delete_event) # Here we connect the "destroy" event to a signal handler. # This event occurs when we call gtk_widget_destroy() on the window, # or if we return FALSE in the "delete_event" callback. self.window.connect("destroy", self.destroy) # Sets the border width of the window. self.window.set_border_width(10) # Creates a new button with the label "Hello World". self.button = gtk.Button("Hello World") # When the button receives the "clicked" signal, it will call the # function hello() passing it None as its argument. The hello() # function is defined above. self.button.connect("clicked", self.hello, None) # This will cause the window to be destroyed by calling # gtk_widget_destroy(window) when "clicked". Again, the destroy # signal could come from here, or the window manager. self.button.connect_object("clicked", gtk.Widget.destroy, self.window) # This packs the button into the window (a GTK container). self.window.add(self.button) # The final step is to display this newly created widget. self.button.show() # and the window self.window.show() def main(self): # All PyGTK applications must have a gtk.main(). Control ends here # and waits for an event to occur (like a key press or mouse event). gtk.main() # If the program is run directly or passed as an argument to the python # interpreter then create a HelloWorld instance and show it if __name__ == "__main__": hello = HelloWorld() hello.main()