Python note (1)

Source: Internet
Author: User

GTK + is developed in C language, but its designers use object-oriented technology. C ++ (gtkmm), Perl, Ruby, Java, and Python (PyGTK) bindings are also provided. Other bindings include Ada, D, Haskell, PHP, and all. NET programming language, GTK + is also flexible, first used in X Window System, has now been transplanted to other platforms, such as Microsoft Windows, DirectFB, and Quartz in Mac OS X. PyGTK is truly cross-platform. It can run on various operating systems without modification, such as Linux, Windows, and MacOS. in addition to ease-of-use and fast prototype development capabilities, PyGTK also has unique features for processing localization languages. PyGTK is a free software, so you can use, modify, distribute, and study it with almost no restrictions. It is released based on the LGPL protocol, which is very friendly to commercial applications.

The following is its helloworld


[Python]
Import gtk
Window = gtk. Window ()
Window. set_title ("Hello World ")
Window. show_all ()
 
Gtk. main ()

Import gtk
Window = gtk. Window ()
Window. set_title ("Hello World ")
Window. show_all ()

Gtk. main ()

Let's look at a standard window.
[Python]
#! /Usr/bin/env python
 
Import pygtk
Pygtk. require ('2. 0 ')
Import gtk
Class Base:
Def _ init _ (self ):
Self. window = gtk. Window (gtk. WINDOW_TOPLEVEL)
Self. window. show ()
Def main (self ):
Gtk. main ()
Print _ name __
If _ name _ = "_ main __":
Base = Base ()
Base. main ()

#! /Usr/bin/env python

Import pygtk
Pygtk. require ('2. 0 ')
Import gtk
Class Base:
Def _ init _ (self ):
Self. window = gtk. Window (gtk. WINDOW_TOPLEVEL)
Self. window. show ()
Def main (self ):
Gtk. main ()
Print _ name __
If _ name _ = "_ main __":
Base = Base ()
Base. main () the first line #! /Usr/bin/env python specifies that the program is parsed and run by python.

Row 5-6:

 

Import pygtk

Pygtk. require ('2. 0 ')

Import gtk

Differentiate different versions of pygtk installed in the system. These lines define the main version of version 2 of pygtk to be used to prevent earlier versions installed in the system from being used, and start the initialization process of GTK +, including gtk_init ()
Row 9-15
Class Base:
Def _ init _ (self ):
Self. window = gtk. Window (gtk. WINDOW_TOPLEVEL)
Self. window. show ()
Def main (self ):
Gtk. main ()


In def _ init _ (self), the _ init _ () function creates a top-level window and directs GTK + to display it: self. window. show ()
 

 

Self. window = gtk. window (gtk. WINDOW_TOPLEVEL), using gtk. the WINDOW_TOPLEVEL parameter defines the decoration style and position of the Form in window management. The default value is 200*200 for a window without a subwindow.
 

Lines 14-15:

Def main (self ):
Gtk. main ()
Line 14-15 defines main () and calls the main () function of PYGTK, including the control of mouse and keyboard events and window events during the gtk + main event loop Process
 

 

 

Line 18-20:
If _ name _ = "_ main __":
Base = Base ()
Base. main ()
Make sure that the program runs directly by PYTHON, instead of importing the program to the python interpreter, the new Base class instance is created and the parameter is stored in the base variable, it will include the main () method to start the gtk + event processing loop.
These lines enable the program to start automatically. If the program is directly called or run as the parameter f of the python interpreter, the program name included in the python Variable _ name _ Will Be _ main __, and 18-20 lines of code will be executed. If the program uses the import Statement and is loaded to a running python virtual machine, 18-20 rows are not executed.
 

Row 19th creates a Base class instance called base. As the execution result, a gtk. window is created and displayed,

Line 3 calls the main () method of the Base class and starts a GTK + event processing cycle. When the controller detects an event, GTK + waits for the occurrence of the x event. In the above Code, event ignored

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.