个人工具

“UbuntuHelp:PythonRecipes/HelloWorld”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(新页面: {{From|https://help.ubuntu.com/community/PythonRecipes/HelloWorld}} {{Languages|UbuntuHelp:PythonRecipes/HelloWorld}} #title Hello World! ''This Python Recipe is currently being written''...)
 
第3行: 第3行:
 
#title Hello World!
 
#title Hello World!
 
''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/PythonRecipes/HelloWorld?action=AttachFile&do=get&target=hello-world.png
 
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
 
# This is a callback function. The data arguments are ignored
 
# in this example. More on callbacks below.
 
# in this example. More on callbacks below.
 
def hello(self, widget, data=None):
 
def hello(self, widget, data=None):
 
print "Hello World"
 
print "Hello World"
 
 
def delete_event(self, widget, event, data=None):
 
def delete_event(self, widget, event, data=None):
 
# If you return FALSE in the "delete_event" signal handler,
 
# If you return FALSE in the "delete_event" signal handler,
第37行: 第26行:
 
# type dialogs.
 
# type dialogs.
 
print "delete event occurred"
 
print "delete event occurred"
 
 
# Change FALSE to TRUE and the main window will not be destroyed
 
# Change FALSE to TRUE and the main window will not be destroyed
 
# with a "delete_event".
 
# with a "delete_event".
 
return False
 
return False
 
 
def destroy(self, widget, data=None):
 
def destroy(self, widget, data=None):
 
print "destroy signal occurred"
 
print "destroy signal occurred"
 
gtk.main_quit()
 
gtk.main_quit()
 
 
def <u>init</u>(self):
 
def <u>init</u>(self):
 
# create a new window
 
# create a new window
 
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
 
# When the window is given the "delete_event" signal (this is given
 
# When the window is given the "delete_event" signal (this is given
 
# by the window manager, usually by the "close" option, or on the
 
# by the window manager, usually by the "close" option, or on the
第56行: 第41行:
 
# function is NULL and is ignored in the callback function.
 
# function is NULL and is ignored in the callback function.
 
self.window.connect("delete_event", self.delete_event)
 
self.window.connect("delete_event", self.delete_event)
 
 
# Here we connect the "destroy" event to a signal handler.   
 
# Here we connect the "destroy" event to a signal handler.   
 
# This event occurs when we call gtk_widget_destroy() on the window,
 
# This event occurs when we call gtk_widget_destroy() on the window,
 
# or if we return FALSE in the "delete_event" callback.
 
# or if we return FALSE in the "delete_event" callback.
 
self.window.connect("destroy", self.destroy)
 
self.window.connect("destroy", self.destroy)
 
 
# Sets the border width of the window.
 
# Sets the border width of the window.
 
self.window.set_border_width(10)
 
self.window.set_border_width(10)
 
 
# Creates a new button with the label "Hello World".
 
# Creates a new button with the label "Hello World".
 
self.button = gtk.Button("Hello World")
 
self.button = gtk.Button("Hello World")
 
 
# When the button receives the "clicked" signal, it will call the
 
# When the button receives the "clicked" signal, it will call the
 
# function hello() passing it None as its argument.  The hello()
 
# function hello() passing it None as its argument.  The hello()
 
# function is defined above.
 
# function is defined above.
 
self.button.connect("clicked", self.hello, None)
 
self.button.connect("clicked", self.hello, None)
 
 
# This will cause the window to be destroyed by calling
 
# This will cause the window to be destroyed by calling
 
# gtk_widget_destroy(window) when "clicked".  Again, the destroy
 
# gtk_widget_destroy(window) when "clicked".  Again, the destroy
 
# signal could come from here, or the window manager.
 
# signal could come from here, or the window manager.
 
self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
 
self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
 
 
# This packs the button into the window (a GTK container).
 
# This packs the button into the window (a GTK container).
 
self.window.add(self.button)
 
self.window.add(self.button)
 
 
# The final step is to display this newly created widget.
 
# The final step is to display this newly created widget.
 
self.button.show()
 
self.button.show()
 
 
# and the window
 
# and the window
 
self.window.show()
 
self.window.show()
 
 
def main(self):
 
def main(self):
 
# All PyGTK applications must have a gtk.main(). Control ends here
 
# 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).
 
# and waits for an event to occur (like a key press or mouse event).
 
gtk.main()
 
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

2007年11月30日 (五) 21:11的版本

  1. title Hello World!

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

HelloWorld?action=AttachFile&do=get&target=hello-world.png

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 <u>init</u>(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 <u>name</u> == "<u>main</u>":
hello = HelloWorld()
hello.main()

Links