PyGTK tutorial 您所在的位置:网站首页 iconiconiconicon PyGTK tutorial

PyGTK tutorial

2023-02-12 10:40| 来源: 网络整理| 查看: 265

PyGTK tutorial

From: http://www.zetcode.com/tutorials/pygtktutorial/

Introduction

First steps

Layout management

Menus

Toolbars

Signals & events

Widgets

Widgets II

Advanced Widgets

Dialogs

Pango

Pango II

Drawing with cairo

Drawing with cairo II

Snake

Custom widget

This is PyGTK tutorial. The PyGTK tutorial is suitable for beginner and

more advanced programmers.

http://www.zetcode.com/tutorials/pygtktutorial/

Introduction to PyGTK

In this part of the PyGTK programming tutorial, we will talk about the

PyGTK GUI library and Python programming language in general.

AboutAboutAboutAbout thisthisthisthis tutorialtutorialtutorialtutorial

This is PyGTK programming tutorial. It has been created and tested on Linux.

The PyGTK programming tutorial is suited for novice and more advanced

programmers.

PyGTKPyGTKPyGTKPyGTK

PyGTK is a set of Python wrappers for the GTK+ GUI library. It offers a

comprehensive set of graphical elements and other useful programming

facilities for creating desktop applications. It is a part of the GNOME

project. PyGTK is free software and licensed under the LGPL. Original

autor of PyGTK is James Henstridge. PyGTK is very easy to use, it is ideal

for rapid prototyping. Currently, PyGTK is one of the most popular

bindings for the GTK+ library.

PyGTK consists of several modules.

GObject is a base class providing the common attributes and functions for

PyGTK classes. ATK is the accessibility toolkit. This toolkit provides

tools which help physically challenged people work with computers. GTK

is the user interface module. The Pango is a library which is used to work

with text and internationalization. Cairo is a library for creating 2D

vector graphics. Glade is used to build GUI interfaces from XML

descriptions.

PythonPythonPythonPython

Python is a dynamic object-oriented programming

language. It is a general purpose programming language. It can be used

for many kinds of software development. The design purpose of the Python

language emphasizes programmer productivity and code readability. Python

was initially developed by Guido van Rossum. It was first released in 1991.

Python was inspired by ABC, Haskell, Java, Lisp, Icon and Perl programming

languages. Python is a high level, general purpose, multiplatform,

interpreted language. Python is a minimalistic language. One of it's most

visible features is that it does not use semicolons nor brackets. Python

uses indentation instead. There are two main branches of Python currently.

Python 2.x and Python 3.x. Python 3.x breaks backward compatibility with

previous releases of Python. It was created to correct some design flaws

of the language and make the language more clean. The most recent version

of Python 2.x is 2.7.1, and of Python 3.x 3.1.3. This tutorial is writtein

in Python 2.x. Today, Python is maintained by a large group of volunteers

worldwide.

GTK+GTK+GTK+GTK+

The GTK+ is a library for creating graphical user interfaces.

The library is created in C programming language. The GTK+ library is also

called the GIMP Toolkit. Originally, the library was created while

developing the GIMP image manipulation program. Since then, the GTK+

became one of the most popular toolkits under Linux and BSD Unix. Today,

most of the GUI software in the open source world is created in Qt or in

GTK+. The GTK+ is an object oriented application programming interface.

The object oriented system is created with the Glib object system, which

is a base for the GTK+ library. The GObject also enables to create language

bindings for various other programming languages. Language bindings exist

for C++, Python, Perl, Java, C# and other programming languages.

Gnome and XFce desktop environments have been created using the GTK+

library. SWT and wxWidgets are well known programming frameworks, that

use GTK+. Prominent software applications that use GTK+ include Firefox

or Inkscape.

SourcesSourcesSourcesSources

pygtk.org wikipedia.org

First steps in PyGTK

In this part of the PyGTK programming tutorial, we will do our first steps

in programming. We will create simple programs.

SimpleSimpleSimpleSimple exampleexampleexampleexample

The first code example is a very simple one.

center.py

#!/usr/bin/python# ZetCode PyGTK tutorial## This is a trivial PyGTK example## author: jan bodnar# website: zetcode.com# last edited: February 2009import gtkclass PyApp(gtk.Window):

def __init__(self):super(PyApp, self).__init__()

http://www.pygtk.orghttp://wwww.wikipedia.org

self.connect("destroy", gtk.main_quit)self.set_size_request(250, 150)self.set_position(gtk.WIN_POS_CENTER)self.show()

PyApp()gtk.main()

This code shows a centered window.

import gtk

We import the gtk module. Here we have objects to create GUI applications.

class PyApp(gtk.Window):

Our application is based on the PyApp class. It inherits from the Window.

def __init__(self):super(PyApp, self).__init__()

This is the constructor. It builds our application. It also calls it's

parent constructor through the super() call.

self.connect("destroy", gtk.main_quit)

We connect the destroy signal to the main_quit() function. The destroy

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 main_quit() we quit the application for good.

self.set_size_request(250, 150)

We set the size of the window to 250x150px.

self.set_position(gtk.WIN_POS_CENTER)

This line centers the window on the screen.

self.show()

Now we show the window. The window is not visible, until we call the show()

method.

PyApp()gtk.main()

We create the instance of our program and start the main loop.

IconIconIconIcon

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.

icon.py

#!/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, sys

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.messagesys.exit(1)

self.connect("destroy", gtk.main_quit)

self.show()

PyApp()gtk.main()

The code example shows the application icon.

self.set_title("Icon")

We set a title for the window.

self.set_icon_from_file("web.png")

The set_icon_from_file() method sets an icon for the window. The image

is loaded from disk in the current working directory.

Figure: Icon

ButtonsButtonsButtonsButtons

In the next example, we will further enhance our programming skills with

the PyGTK library.

buttons.py

#!/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()

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.

btn1 = gtk.Button("Button")

A Button is a child widget. Child widgets are placed inside containers.

btn1.set_sensitive(False)

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.

btn3 = gtk.Button(stock=gtk.STOCK_CLOSE)

The third button shows an image inside it's area. The PyGTK library has

a built-in stock of images, that we can use.

btn4.set_size_request(80, 40)

Here we change the size of the button.

fixed = gtk.Fixed()

Fixed widget is a non visible container widget. It's purpose is to contain

other child widgets.

fixed.put(btn1, 20, 30)fixed.put(btn2, 100, 30)...

Here we place button widgets inside fixed container widget.

self.add(fixed)

We set the Fixed container to be the main container for our Window widget.

self.show_all()

We can either call show_all() method, or we call show() method on each

of the widgets. Including containers.

Figure: Buttons

TooltipTooltipTooltipTooltip

A tooltip is a hint on a widget in the applications. Can be used to provide

additional help.

tooltips.py

#!/usr/bin/python

# ZetCode PyGTK tutorial## This code shows a tool



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有