PyGTK FAQ First steps

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(исправил название категории)
(Удалено по требованию автора...)
 
Строка 1: Строка 1:
-
<h1>First steps in PyGTK</h1>
 
-
In this part of the PyGTK programming tutorial, we will do our first steps in programming.
 
-
We will create simple programs.
 
-
 
-
== Simple example ==
 
-
The first code example is a very simple one.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# ZetCode PyGTK tutorial
 
-
#
 
-
# This is a trivial PyGTK example
 
-
#
 
-
# author: jan bodnar
 
-
# website: zetcode.com
 
-
# last edited: February 2009
 
-
 
-
 
-
import gtk
 
-
 
-
class PyApp(gtk.Window):
 
-
    def __init__(self):
 
-
        super(PyApp, self).__init__()
 
-
       
 
-
        self.connect("destroy", gtk.main_quit)
 
-
        self.set_size_request(250, 150)
 
-
        self.set_position(gtk.WIN_POS_CENTER)
 
-
        self.show()
 
-
 
-
PyApp()
 
-
gtk.main()
 
-
</source>
 
-
 
-
This code shows a centered window.
 
-
 
-
<source lang="python">
 
-
import gtk
 
-
</source>
 
-
 
-
We import the gtk module. Here we have objects to create GUI applications.
 
-
 
-
<source lang="python">
 
-
class PyApp(gtk.Window):
 
-
</source>
 
-
 
-
Our application is based on the <b>PyApp</b> class. It inherits from the <b>Window</b>.
 
-
 
-
<source lang="python">
 
-
def __init__(self):
 
-
    super(PyApp, self).__init__()
 
-
</source>
 
-
 
-
This is the constructor. It builds our application. It also calls it's parent constructor through the <b>super()</b> call.
 
-
 
-
<source lang="python">
 
-
self.connect("destroy", gtk.main_quit)
 
-
</source>
 
-
 
-
We connect the <b>main_quit()</b> function.
 
-
The <b>destroy</b> signal is called when we click on the close
 
-
button in the titlebar or press Alt + F4. The window is being destroyed, but the application is not. You can see it, if you launch the example from the command line.
 
-
By calling the <b>main_quit()</b> we quit the application for good.
 
-
 
-
<source lang="python">
 
-
 
-
self.set_size_request(250, 150)
 
-
</source>
 
-
 
-
We set the size of the window to 250x150px.
 
-
 
-
<source lang="python">
 
-
self.set_position(gtk.WIN_POS_CENTER)
 
-
</source>
 
-
 
-
This line centers the window on the screen.
 
-
 
-
<source lang="python">
 
-
self.show()
 
-
 
-
</source>
 
-
 
-
Now we show the window. The window is not visible, until we call the <b>show()</b> method.
 
-
 
-
<source lang="python">
 
-
PyApp()
 
-
gtk.main()
 
-
</source>
 
-
 
-
We create the instance of our program and start the main loop.
 
-
 
-
== Icon ==
 
-
 
-
In the next example, we show the application icon. Most window managers display the icon in the left corner of the titlebar and also on the taskbar.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# ZetCode PyGTK tutorial
 
-
#
 
-
# This example shows an icon
 
-
# in the titlebar of the window
 
-
#
 
-
# author: jan bodnar
 
-
# website: zetcode.com
 
-
# last edited: February 2009
 
-
 
-
 
-
import gtk
 
-
 
-
class PyApp(gtk.Window):
 
-
    def __init__(self):
 
-
        super(PyApp, self).__init__()
 
-
       
 
-
        self.set_title("Icon")
 
-
        self.set_size_request(250, 150)
 
-
        self.set_position(gtk.WIN_POS_CENTER)
 
-
 
-
        try:
 
-
            self.set_icon_from_file("web.png")
 
-
        except Exception, e:
 
-
            print e.message
 
-
            sys.exit(1)
 
-
 
-
        self.connect("destroy", gtk.main_quit)
 
-
 
-
        self.show()
 
-
 
-
PyApp()
 
-
gtk.main()
 
-
 
-
</source>
 
-
 
-
The code example shows the application icon.
 
-
 
-
<source lang="python">
 
-
self.set_title("Icon")
 
-
</source>
 
-
 
-
We set a title for the window.
 
-
 
-
<source lang="python">
 
-
self.set_icon_from_file("web.png")
 
-
 
-
</source>
 
-
 
-
The <b>set_icon_from_file()</b> method sets an icon for the window. The image is loaded from disk in the current working directory.
 
-
 
-
[[image: pygtk_faq_icon.png | center]]
 
-
 
-
== Buttons ==
 
-
 
-
In the next example, we will further enhance our programming skills with  the PyGTK library.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
 
-
# ZetCode PyGTK tutorial
 
-
#
 
-
# This example shows four buttons
 
-
# in various modes
 
-
#
 
-
# author: jan bodnar
 
-
# website: zetcode.com
 
-
# last edited: February 2009
 
-
 
-
 
-
import gtk
 
-
 
-
class PyApp(gtk.Window):
 
-
    def __init__(self):
 
-
        super(PyApp, self).__init__()
 
-
       
 
-
        self.set_title("Buttons")
 
-
        self.set_size_request(250, 200)
 
-
        self.set_position(gtk.WIN_POS_CENTER)
 
-
       
 
-
        btn1 = gtk.Button("Button")
 
-
        btn1.set_sensitive(False)
 
-
        btn2 = gtk.Button("Button")
 
-
        btn3 = gtk.Button(stock=gtk.STOCK_CLOSE)
 
-
        btn4 = gtk.Button("Button")
 
-
        btn4.set_size_request(80, 40)
 
-
 
-
        fixed = gtk.Fixed()
 
-
 
-
        fixed.put(btn1, 20, 30)
 
-
        fixed.put(btn2, 100, 30)
 
-
        fixed.put(btn3, 20, 80)
 
-
        fixed.put(btn4, 100, 80)
 
-
       
 
-
        self.connect("destroy", gtk.main_quit)
 
-
       
 
-
        self.add(fixed)
 
-
        self.show_all()
 
-
 
-
 
-
PyApp()
 
-
gtk.main()
 
-
</source>
 
-
 
-
We show four different buttons on the window. We will see a difference between container widgets and child widgets and will change some properties of child widgets.
 
-
 
-
 
-
<source lang="python">
 
-
btn1 = gtk.Button("Button")
 
-
</source>
 
-
 
-
A <b>Button</b> is a child widget. Child widgets are placed inside containers.
 
-
 
-
<source lang="python">
 
-
btn1.set_sensitive(False)
 
-
 
-
</source>
 
-
 
-
We make this button insensitive. This means, we cannot click on it.  Nor it can be selected, focused etc. Graphically the widget is grayed out.
 
-
 
-
<source lang="python">
 
-
btn3 = gtk.Button(stock=gtk.STOCK_CLOSE)
 
-
</source>
 
-
 
-
The third button shows an image inside it's area. The PyGTK library has a built-in stock of images, that we can use.
 
-
 
-
<source lang="python">
 
-
btn4.set_size_request(80, 40)
 
-
 
-
</source>
 
-
 
-
Here we change the size of the button.
 
-
 
-
<source lang="python">
 
-
fixed = gtk.Fixed()
 
-
</source>
 
-
 
-
<b>Fixed</b> widget is a non visible container widget. It's purpose is to contain other child widgets.
 
-
 
-
<source lang="python">
 
-
fixed.put(btn1, 20, 30)
 
-
fixed.put(btn2, 100, 30)
 
-
...
 
-
</source>
 
-
 
-
Here we place button widgets inside fixed container widget.
 
-
 
-
<source lang="python">
 
-
self.add(fixed)
 
-
</source>
 
-
 
-
We set the <b>Fixed</b> container to be the main container for our <b>Window</b> widget.
 
-
 
-
<source lang="python">
 
-
self.show_all()
 
-
</source>
 
-
 
-
We can either call <b>show()</b> method on each of the widgets. Including containers.
 
-
 
-
[[image: pygtk_faq_buttons.png | center]]
 
-
 
-
== Tooltip ==
 
-
 
-
A tooltip is a hint on a widget in the applications. Can be used to provide additional help.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# ZetCode PyGTK tutorial
 
-
#
 
-
# This code shows a tooltip on
 
-
# a window and a button
 
-
#
 
-
# author: jan bodnar
 
-
# website: zetcode.com
 
-
# last edited: February 2009
 
-
 
-
 
-
import gtk
 
-
 
-
class PyApp(gtk.Window):
 
-
 
-
    def __init__(self):
 
-
        super(PyApp, self).__init__()
 
-
     
 
-
        self.set_title("Tooltips")
 
-
        self.set_size_request(250, 200)
 
-
        self.set_position(gtk.WIN_POS_CENTER)
 
-
 
-
        self.connect("destroy", gtk.main_quit)
 
-
 
-
        self.fixed = gtk.Fixed()
 
-
        self.add(self.fixed)
 
-
     
 
-
        button = gtk.Button("Button")
 
-
        button.set_size_request(80, 35)     
 
-
 
 
-
        self.fixed.put(button, 50, 50)
 
-
     
 
-
        self.set_tooltip_text("Window widget")
 
-
        button.set_tooltip_text("Button widget")
 
-
 
-
        self.show_all()
 
-
 
-
PyApp()
 
-
gtk.main()
 
-
</source>
 
-
 
-
In this example we set a tooltip for a window and for a button.
 
-
 
-
<source lang="python">
 
-
self.set_tooltip_text("Window widget")
 
-
button.set_tooltip_text("Button widget")
 
-
</source>
 
-
 
-
The <b>set_tooltip_text()</b> does the job.
 
-
 
-
[[image: pygtk_faq_tooltips.png | center]]
 
-
 
-
In this chapter, we created first programs in PyGTK programming library.
 
-
 
-
[[Категория:Python]]
 
-
[[Категория:GTK+]]
 

Текущая версия на 11:24, 7 апреля 2009