运行结果如下图
代码如下
# //
# // _oo0oo_
# // o8888888o
# // 88" . "88
# // (| -_- |)
# // 0\ = /0
# // ___/`---'\___
# // .' \\| |// '.
# // / \\||| : |||// \
# // / _||||| -:- |||||- \
# // | | \\\ - /// | |
# // | \_| ''\---/'' |_/ |
# // \ .-\__ '-' ___/-. /
# // ___'. .' /--.--\ `. .'___
# // ."" '< `.___\_<|>_/___.' >' "".
# // | | : `- \`.;`\ _ /`;.`/ - ` : | |
# // \ \ `_. \_ __\ /__ _/ .-` / /
# // =====`-.____`.___ \_____/___.-`___.-'=====
# // `=---='
# //
# //
# // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# //
# // 佛祖保佑 永无BUG
# //
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QWidget, QLCDNumber,
QApplication,QPushButton)
#打开txt文件, 用来读数据并存储数据
with open("calData.txt","r") as file2:
num = int(file2.read())
class Example(QWidget):
lcd = None
def __init__(self):
super().__init__()
self.lcd = QLCDNumber(self)
self.initUI()
#//button触发后要执行的函数
def buttonClick(self):
global num
num = num+1
self.lcd.display(str(num))
def buttonClick3(self):
global num
num = num+2
self.lcd.display(str(num))
def buttonClick4(self):
global num
num = 0
self.lcd.display(str(num))
def initUI(self):
#初始化界面
self.resize(300,150)
self.lcd.setGeometry(0, 0, 300, 100)
self.lcd.display(str(num))
#初始化按钮
btn2 = QPushButton("加1", self)
btn2.resize(btn2.sizeHint())
btn2.move(3,110)
btn2.clicked.connect(self.buttonClick)
btn3 = QPushButton("加2", self)
btn3.resize(btn3.sizeHint())
btn3.move(103, 110)
btn3.clicked.connect(self.buttonClick3)
btn4 = QPushButton("清零", self)
btn4.resize(btn4.sizeHint())
btn4.move(203, 110)
btn4.clicked.connect(self.buttonClick4)
#设置图标
self.setWindowTitle("icon")
self.setWindowIcon(QIcon('teacher.png'))
self.show()
if __name__ == '__main__':
app = QApplication([])
ex = Example()
app.exec_()
#关闭文件并将num中的值存入txt中, 供下一次使用
file2.close()
with open("calData.txt","w") as file3:
file3.write(str(num))
file3.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)