This is a simple example showing a small window. Yet we can do a lot with this window. We can resize it, maximise it, or minimise it. This requires a lot of coding. Someone already coded this functionality. Because it repeats in most applications, there is no need to code it over again. PyQt4 is a high level toolkit. If we would code in a lower level toolkit, the following code example could easily have hundreds of lines.

#!/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
""" import sys
from PyQt4 import QtGui def main(): app = QtGui.QApplication(sys.argv) w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show() sys.exit(app.exec_()) if __name__ == '__main__':
main()

The above code shows a small window on the screen.

import sys
from PyQt4 import QtGui

Here we provide the necessary imports. The basic GUI widgets are located in the QtGui module.

app = QtGui.QApplication(sys.argv)

Every PyQt4 application must create an application object. The application object is located in theQtGui 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.

w = QtGui.QWidget()

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.resize(250, 150)

The resize() method resizes the widget. It is 250px wide and 150px high.

w.move(300, 300)

The move() method moves the widget to a position on the screen at x=300 and y=300 coordinates.

w.setWindowTitle('Simple')

Here we set the title for our window. The title is shown in the titlebar.

w.show()

The show() method displays the widget on the screen. A widget is first created in memory and later shown on the screen.

sys.exit(app.exec_())

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.

Figure: Simple

Simple example的更多相关文章

  1. PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)

    最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...

  2. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  3. WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION

    开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...

  4. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  5. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  6. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  7. 设计模式之简单工厂模式Simple Factory(四创建型)

    工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...

  8. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  9. 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...

    在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...

  10. A Simple OpenGL Shader Example II

    A Simple OpenGL Shader Example II eryar@163.com Abstract. The OpenGL Shading Language syntax comes f ...

随机推荐

  1. Date与String间的转换

    一.Date-->String :格式化过程 1. DateFormat :String format(Date d) 2.SimpleDateFormat是继承DateFormat(抽象类)的 ...

  2. 华为S5300系列交换机V100R006SPH017VRP热补丁

    S5300_V100R006SPH017.pat 附件: 链接:https://pan.baidu.com/s/16lrNMykatXR3_5xKBc2zuw  密码:rt1l

  3. GoodSync 同步 对比 备份

    单向/双向皆可,针对任何一侧的文件进行处理,两侧文件都进行更新 文件同步 的操作过程是:确保两处或多处包含完全一致.及时更新 的各种文件.当对某一处进行文件添加.更改或删除,则同步操作将对对应的 另一 ...

  4. c# 四舍五入、上取整、下取整

    在处理一些数据时,我们希望能用“四舍五入”法实现,但是C#采用的是“四舍六入五成双”的方法,如下面的例子,就是用“四舍六入五成双”得到的结果: double d1 = Math.Round(1.25, ...

  5. 订阅Linux内核邮件列表

    http://www.wowotech.net/sort/linux_application/lkml.html https://kernelnewbies.org/ML https://lkml.o ...

  6. linux 内核大牛-谢宝友

    http://blog.chinaunix.net/uid/25845340.html 谢宝友:毕业于四川省税务学校税收专业,现供职于中兴通讯操作系统团队,对操作系统内核有较强的兴趣.专职于操作系统内 ...

  7. SHP文件合并

    ArcGIS中合并SHP文件是一个常用的操作,下面简要讲解一下如何合并. 使用ArcGIS Tool Box(ArcGIS工具箱)中的Data Management Tools-->Genera ...

  8. 【linux】centos7终端中文显示乱码,命令返回中文乱码

    centos7终端中文显示乱码,命令返回中文乱码 1.查看服务器编码的命令 1.1 echo $LANG 1.2 locale 1.3 查看终端xshell编码 如果以上的三点依旧保持一致,而依旧乱码 ...

  9. 2012年及之后的ImageNet比赛的冠军、亚军和季军ImageNet winners after 2012

    2012 0.15 - Supervision (AlexNet) - ~ 60954656 params 0.26 - ISI (ensemble of features) 0.27 - LEAR ...

  10. DELPHI中千万别直接使用CreateThread ,建议使用BeginThread(在C++中无大问题,可是到了DELPHI中情况就不一样了)

    以前在写个别程序的时候老是喜欢使用纯API编程. 在C++中无大问题,可是到了DELPHI中情况就不一样了. 当你用 DELPHI写的多线程程序莫名其妙的内存错误,特别是字符串(string)操作;  ...