Simple example
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的更多相关文章
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION
开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- 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 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- 设计模式之简单工厂模式Simple Factory(四创建型)
工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- 关于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交叉 ...
- A Simple OpenGL Shader Example II
A Simple OpenGL Shader Example II eryar@163.com Abstract. The OpenGL Shading Language syntax comes f ...
随机推荐
- JavaMail_测试编写
@Test public void test1() throws Exception{ // import java.util.Properties; // import javax.mail.Add ...
- 自动化运维之 ~cobbler~
一 .Cobbler简介 Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows.该工具使用python开发,小巧轻便(才15k 行python代码),使用简 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- LoadRunner的简单使用《第一篇》
LoadRunner是一个用压力测试的软件.这东西比较难上手,光安装就非常麻烦.好不容易一步步跟着安装说明安装好之后,还是用不了. 记录一个问题如下: 导入脚本的时候报错fail to create ...
- 用显微镜观察cpu芯片内部
1. 先找到一块Intel公司的奔三(Pentium III)Coppermine芯片,主频800MHZ,生产于2000年.(我查了一下,网上的报价现在是15~30元人民币/块.) 下面是这块CPU的 ...
- .NET泛型01,为什么需要泛型,泛型基本语法
.NET泛型或许是借鉴于C++泛型模版,借助它可以实现对类型的抽象化.泛型处理,实现了类型和方法之间的解耦.一个最经典的运用是在三层架构中,针对不同的领域模型,在基接口.基类中实现针对各个领域模型的泛 ...
- remotepath != null 与 !TextUtils.isEmpty(remotepath) 的差别
remotepath != null 与 !TextUtils.isEmpty(remotepath) 的差别 !TextUtils.isEmpty(remotepath) 与 remo ...
- iOS宏(自己使用,持续更新)
// 直接从RGB取颜色(RGB 0xFF00FF)#define UICOLOR_FROM_RGB(rgbValue) \[UIColor colorWithRed:((float)((rgbVal ...
- Struts2 无后缀action请求
如果将Struts2的filter-mapping配置成 <filter-mapping> <filter-name>struts2</filter-name> & ...
- Linux 系统分析命令图
sar命令: 编辑 /etc/default/sysstat 设置为true sudo /etc/init.d/sysstat restart