这两天一直在研究程序模块之类的问题,天今正好有机会和大家共享一下.
天今我们开个小差不看 《爱可的Python》。
荐推一本书 《Rapid GUI Programming with Python and Qt 》 网上英文版下载一大堆,大家可以自己去找。
天今我们讲一下面里第一个Python写的GUI小程序。
一个小闹钟,只是屏幕提示。
让我们先分析这个闹钟如何奇葩。
- 须要通过命令行动启。
- 没有标题栏。
- 没单菜。
- 甚至没有关闭按钮。
- 没有运行面界。
看到这里想必大家要问了,何必去做一个这样弱爆了的程序呢。明显,育教义意大于其际实用途。
像其他的模块一样,QT面界包是须要载加的。
我们用到了命令行入输,所以,sys模块也是必须的。
用到了时光,但是我们不须要用使time模块,我们用QT供提的QTime模块。
让我们先写import 语句分部。
import sys
from PyQt4.QtCore import * from PyQt4.QtGui import *
安分守纪的抄上就成了。
还有一句话要写。
app = QApplication(sys.argv)
这是一个控制qt程序脉命的类,后之我们会渐渐解释。在每个Qt程序中,都市找到类似的语句。
后之就是入输闹钟时光了。上程序。
try: message = "Alert!" if len(sys.argv) < 2: raise ValueError hours, mins = sys.argv[1].split(":") due = QTime(int(hours), int(mins)) if not due.isValid(): raise ValueError if len(sys.argv) > 2: message = " ".join(sys.argv[2:]) except ValueError: message = "Usage: alert.pyw HH:MM [optional message]" # 24hr clock
其中包含了异常处理,对于一个应用程序,我们应当将全部的异常情况都虑考进去。
程序中用使raise 来触发异常。迫使程序进入我们的想预轨道。
明显,面上的程序是不敷的,它并没有时光判断和GUI分部。
如何判断时光是不是到了呢,我们给出一个不是特殊精确的方法,时定判断。
import time while QTime.currentTime() < due: time.sleep(20) # 20 seconds
当时光到了我们就会跳到下一步,创立一个窗体,迟延60s 后关闭。
label = QLabel("<font color=red size=72><b>" + message + "</b></font>") label.setWindowFlags(Qt.SplashScreen) label.show() QTimer.singleShot(60000, app.quit) # 1 minute app.exec_()
看来,我们又须要写一个关于 QTime模块的分析了。
在现把全部的句子放在一同。让我们测试下。
当我们入输一个错误的时光,统系提示错误。
入输确正的时光后程序运行畸形。
附录: 整完程序
import sys import time from PyQt4.QtCore import * from PyQt4.QtGui import * app = QApplication(sys.argv) try: message = "Alert!" if len(sys.argv) < 2: raise ValueError hours, mins = sys.argv[1].split(":") due = QTime(int(hours), int(mins)) if not due.isValid(): raise ValueError if len(sys.argv) > 2: message = " ".join(sys.argv[2:]) except ValueError: message = "Usage: alert.pyw HH:MM [optional message]" # 24hr clock while QTime.currentTime() < due: time.sleep(20) # 20 seconds label = QLabel("<font color=red size=72><b>" + message + "</b></font>") label.setWindowFlags(Qt.SplashScreen) label.show() QTimer.singleShot(60000, app.quit) # 1 minute app.exec_()
文章结束给大家分享下程序员的一些笑话语录: AdobeFlash拖垮Windows拖垮IE!又拖垮Linux拖垮Ubuntu拖垮FirxEox!还拖垮BSD拖垮MacOS拖垮Safri!简直无所不拖!AdobeFlash滚出网路世界!不要以为市占有率高就可以持续出烂货产品!以后替代品多得是!
相关推荐
《Rapid GUI Programming with Python and Qt》
With Rapid GUI Programming with Python and Qt you'll learn how to build efficient GUI applications that run on all major operating systems, including Windows, Mac OS X, Linux, and many versions of ...
Rapid GUI Programming with Python and Qt 2008 (源代码)
英文原版pdf PYTHON QT GUI快速编程 PYQT编程指南 PYTHON QT GUI快速编程 PYQT编程指南
Part I: Python Programming Chapter 1. Data Types and Data Structures Chapter 2. Control Structures Chapter 3. Classes and Modules Part II: Basic GUI Programming Chapter 4. Introduction to GUI ...
Rapid_GUI_Programming_with_Python_and_Qt_2008.zip, 包括英文版pdf 以及各章源码
Rapid GUI Programming with Python and Qt的随书源码,要的速下
If you are a Python programmer with intermediate level knowledge of GUI programming and want to learn how to create beautiful, effective, and responsive GUIs using the freely available Python GUI ...
GUI Programming with Python GUI Programming with Python
Python is a multi-domain, interpreted programming language. It is a widely used general-purpose, high-level programming language. It is often used as a scripting language because of its forgiving ...
Python GUI Programming Cookbook Second Edition 包含PDF和epub文件 Book Name : Python GUI Programming Cookbook Edition : 2nd Edition | | ISBN : B01N7IOL6S Author Name : Burkhard A Meier; Category : ...
Rapid Gui Programming with python and Qt.pd
PyQt 是基于Python语言结合Qt平台的界面开发软件,此资源包括PDF高清文件和各章程序源码
If you are a Python programmer with intermediate level knowledge of GUI programming and want to learn how to create beautiful, effective, and responsive GUIs using the freely available Python GUI ...
Python Python Python Python Python Python Python Python Python
Rapid GUI programming with python and QT PH