ZetCode PyQt4 tutorial First programs

简介: #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we create a simple window in PyQt4.
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we create a simple
window in PyQt4.

author: Jan Bodnar
website: zetcode.com 
last edited: October 2011
"""
# Here we provide the necessary imports. The basic GUI widgets are located in the QtGui module.
import sys
from PyQt4 import QtGui


def main():
    
    # Every PyQt4 application must create an application object. The application object is located in the QtGui module. The sys.argv parameter is a list of arguments from the command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.
    app = QtGui.QApplication(sys.argv)

    # The QtGui.QWidget widget is the base class of all user interface objects in PyQt4. We provide the default constructor for QtGui.QWidget. The default constructor has no parent. A widget with no parent is called a window.
    w = QtGui.QWidget()
    # The resize() method resizes the widget. It is 250px wide and 150px high.
    w.resize(250, 150)
    # The move() method moves the widget to a position on the screen at x=300 and y=300 coordinates.
    w.move(300, 300)
    # Here we set the title for our window. The title is shown in the titlebar.
    w.setWindowTitle('Simple')
    # The show() method displays the widget on the screen. A widget is first created in memory and later shown on the screen.
    w.show()
    
    # Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit() method or the main widget is destroyed. The sys.exit() method ensures a clean exit. The environment will be informed how the application ended.
    # The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_() was used instead.
    sys.exit(app.exec_())


# When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.
if __name__ == '__main__':
    main()

 

目录
相关文章
|
Shell Perl 网络安全
|
关系型数据库 数据库 Python
7.3 Designing the Python GUI database
设计数据库,创建两个表(Books)存储书名与(Quotations)注释,主要是sql的使用 创建数据库create database databaseName default character set 'utf8'; 显示所有数据库show databases; 显...
1158 0
|
Python
ZetCode PyQt4 tutorial Dialogs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.
830 0
|
Python 前端开发
ZetCode PyQt4 tutorial widgets I
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, a QtGui.
932 0
|
Python Ubuntu Linux
ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we dispay an image on the window.
736 0