1 25行的弹出式闹钟
import sys import time from PyQt5 import QtCore from PyQt5.QtWidgets import QLabel from PyQt5.QtWidgets import QApplication #先创建一个app实例 app = QApplication(sys.argv) # handle the argv try: due = QtCore.QTime.currentTime() message = 'Alert!' if len(sys.argv) < 2: raise ValueError hours, mins = sys.argv[1].split(':') due = QtCore.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: alarm_clock.py HH:MM [optional message]' while QtCore.QTime.currentTime() < due: print(QtCore.QTime.currentTime()) time.sleep(1) #设定标签内容,然后show这个标签 #调用一个单次触发器,6w毫秒(1min)后,触发app.quit。 label = QLabel('<font color=red size=72><b>' + message + '</b></font>') label.show() QtCore.QTimer.singleShot(60000, app.quit) #执行app实例 app.exec_()
2 30行的表达式求值程序
import sys from math import * from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication,QDialog,QLineEdit,QTextBrowser,QVBoxLayout #Form这个class是一个界面 class Form(QDialog): #使用几种方法来做用户响应 def __init__(self,parent=None): super(Form, self).__init__(parent) self.browser = QTextBrowser() self.lineedit = QLineEdit('input the expression and press Enter') self.lineedit.selectAll() #设定布局 layout = QVBoxLayout() layout.addWidget(self.browser) layout.addWidget(self.lineedit) self.setLayout(layout) self.lineedit.setFocus() #设定触发机制 self.lineedit.returnPressed.connect(self.updateUi) #设定title self.setWindowTitle('Calculate') #具体的数学方法 def updateUi(self): try: text = self.lineedit.text() result = eval(text) self.browser.append('%s = %s' %(text,result)) except: self.browser.append('<font color=red>%s is invalid!</font>' %text) app = QApplication(sys.argv) form = Form() form.show() app.exec_()
3 70行货币转换程序
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Form(QDialog): def __init__(self,parent=None): super(Form, self).__init__(parent) self.rates = {'CNY': 1, 'HK': 0.86, 'USD': 6.79} # 创建多选框,并添加多选数据 self.fromComboBox = QComboBox() self.fromComboBox.addItems(self.rates) #创建浮点型数据微调框,并设定精度范围和初始值 self.fromSpinBox = QDoubleSpinBox() self.fromSpinBox.setRange(0.01,100000.00) self.fromSpinBox.setValue(1.00) #第二个多选框,用于计算对应货币 self.toComboBox = QComboBox() self.toComboBox.addItems(self.rates) #创建一个标签,并给定显示内容 self.toLable = QLabel('1.00') #将多个界面构成一个布局 grid = QGridLayout() grid.addWidget(self.fromComboBox, 1, 0) grid.addWidget(self.fromSpinBox,1,1) grid.addWidget(self.toComboBox,2,0) grid.addWidget(self.toLable,2,1) self.setLayout(grid) #只要任意一个变化,就触发函数,用于实时计算结果 #多选框是index变化,浮点数字框是value变化 self.fromComboBox.currentIndexChanged.connect(self.updateUi) self.toComboBox.currentIndexChanged.connect(self.updateUi) self.fromSpinBox.valueChanged.connect(self.updateUi) #设置标题 self.setWindowTitle('Currency') def updateUi(self): #QComboBox有index和text,对应字典中的k-v。当索引变化,就触发text的计算,然后写入toLable(取两位小数点)。 to = self.toComboBox.currentText() from_ = self.fromComboBox.currentText() amount = self.rates[from_] / self.rates[to] * self.fromSpinBox.value() self.toLable.setText('%.2f' %amount) app = QApplication(sys.argv) form = Form() form.show() app.exec_()