大家好,欢迎来到IT知识分享网。
PyQt5+Qt designer 制作计算器
配置说明:
Anaconda 4.2.0 (64-bit)
Python 3.5.2
首先,在Qt designer里面设计界面:
打开 Qt designer后,设计界面如下所示:
保存为clat.ui。然后将其转为py文件:
之后会生产clat.py文件。
打开后,代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'clat.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(746, 455)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(81, 31, 512, 399))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.widget)
self.textBrowser.setObjectName("textBrowser")
self.gridLayout.addWidget(self.textBrowser, 0, 0, 2, 4)
self.calendarWidget = QtWidgets.QCalendarWidget(self.widget)
self.calendarWidget.setObjectName("calendarWidget")
self.gridLayout.addWidget(self.calendarWidget, 1, 4, 1, 2)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 2, 0, 1, 5)
self.pushButton_19 = QtWidgets.QPushButton(self.widget)
self.pushButton_19.setObjectName("pushButton_19")
self.gridLayout.addWidget(self.pushButton_19, 4, 0, 1, 1)
self.pushButton_17 = QtWidgets.QPushButton(self.widget)
self.pushButton_17.setObjectName("pushButton_17")
self.gridLayout.addWidget(self.pushButton_17, 4, 1, 1, 1)
self.pushButton_18 = QtWidgets.QPushButton(self.widget)
self.pushButton_18.setObjectName("pushButton_18")
self.gridLayout.addWidget(self.pushButton_18, 4, 2, 1, 1)
self.pushButton_1 = QtWidgets.QPushButton(self.widget)
self.pushButton_1.setObjectName("pushButton_1")
self.gridLayout.addWidget(self.pushButton_1, 4, 3, 1, 3)
self.pushButton_14 = QtWidgets.QPushButton(self.widget)
self.pushButton_14.setObjectName("pushButton_14")
self.gridLayout.addWidget(self.pushButton_14, 6, 0, 1, 1)
self.pushButton_15 = QtWidgets.QPushButton(self.widget)
self.pushButton_15.setObjectName("pushButton_15")
self.gridLayout.addWidget(self.pushButton_15, 6, 1, 1, 1)
self.pushButton_16 = QtWidgets.QPushButton(self.widget)
self.pushButton_16.setObjectName("pushButton_16")
self.gridLayout.addWidget(self.pushButton_16, 6, 2, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 6, 3, 1, 3)
self.pushButton_11 = QtWidgets.QPushButton(self.widget)
self.pushButton_11.setObjectName("pushButton_11")
self.gridLayout.addWidget(self.pushButton_11, 8, 0, 1, 1)
self.pushButton_12 = QtWidgets.QPushButton(self.widget)
self.pushButton_12.setObjectName("pushButton_12")
self.gridLayout.addWidget(self.pushButton_12, 8, 1, 1, 1)
self.pushButton_13 = QtWidgets.QPushButton(self.widget)
self.pushButton_13.setObjectName("pushButton_13")
self.gridLayout.addWidget(self.pushButton_13, 8, 2, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.widget)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 8, 3, 1, 3)
self.pushButton_8 = QtWidgets.QPushButton(self.widget)
self.pushButton_8.setObjectName("pushButton_8")
self.gridLayout.addWidget(self.pushButton_8, 9, 0, 1, 1)
self.pushButton_9 = QtWidgets.QPushButton(self.widget)
self.pushButton_9.setObjectName("pushButton_9")
self.gridLayout.addWidget(self.pushButton_9, 9, 1, 1, 1)
self.pushButton_10 = QtWidgets.QPushButton(self.widget)
self.pushButton_10.setObjectName("pushButton_10")
self.gridLayout.addWidget(self.pushButton_10, 9, 2, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(self.widget)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 9, 3, 1, 3)
self.pushButton_7 = QtWidgets.QPushButton(self.widget)
self.pushButton_7.setObjectName("pushButton_7")
self.gridLayout.addWidget(self.pushButton_7, 10, 0, 1, 2)
self.pushButton_6 = QtWidgets.QPushButton(self.widget)
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout.addWidget(self.pushButton_6, 10, 2, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.widget)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout.addWidget(self.pushButton_5, 10, 3, 1, 3)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 5, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_19.setText(_translate("Form", "退出"))
self.pushButton_17.setText(_translate("Form", "返回"))
self.pushButton_18.setText(_translate("Form", "清除所有"))
self.pushButton_1.setText(_translate("Form", "/"))
self.pushButton_14.setText(_translate("Form", "7"))
self.pushButton_15.setText(_translate("Form", "8"))
self.pushButton_16.setText(_translate("Form", "9"))
self.pushButton_2.setText(_translate("Form", "*"))
self.pushButton_11.setText(_translate("Form", "4"))
self.pushButton_12.setText(_translate("Form", "5"))
self.pushButton_13.setText(_translate("Form", "6"))
self.pushButton_3.setText(_translate("Form", "+"))
self.pushButton_8.setText(_translate("Form", "1"))
self.pushButton_9.setText(_translate("Form", "2"))
self.pushButton_10.setText(_translate("Form", "3"))
self.pushButton_4.setText(_translate("Form", "-"))
self.pushButton_7.setText(_translate("Form", "0"))
self.pushButton_6.setText(_translate("Form", "."))
self.pushButton_5.setText(_translate("Form", "="))
self.pushButton.setText(_translate("Form", "清除缓存"))
IT知识分享网
然后,新建Run.py文件,代码如下:
IT知识分享网import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from count.clat import Ui_Form
class mwindow(QWidget, Ui_Form):
def __init__(self):
super(mwindow, self).__init__()
self.setupUi(self)
def ps_bt(self):
self.textBrowser.clear()
def ps_bt1(self):
self.lineEdit.insert('/')
def ps_bt2(self):
self.lineEdit.insert('*')
def ps_bt3(self):
self.lineEdit.insert('+')
def ps_bt4(self):
self.lineEdit.insert('-')
def ps_bt5(self):
self.lineEdit.insert('=')
self.calculate()
def ps_bt6(self):
self.lineEdit.insert('.')
def ps_bt7(self):
self.lineEdit.insert('0')
def ps_bt8(self):
self.lineEdit.insert('1')
def ps_bt9(self):
self.lineEdit.insert('2')
def ps_bt10(self):
self.lineEdit.insert('3')
def ps_bt11(self):
self.lineEdit.insert('4')
def ps_bt12(self):
self.lineEdit.insert('5')
def ps_bt13(self):
self.lineEdit.insert('6')
def ps_bt14(self):
self.lineEdit.insert('7')
def ps_bt15(self):
self.lineEdit.insert('8')
def ps_bt16(self):
self.lineEdit.insert('9')
def ps_bt17(self):
self.lineEdit.backspace()
def ps_bt18(self):
self.lineEdit.clear()
def ps_bt19(self):
self.close()
def lineEdit_clear(self):
self.lineEdit.clear()
def calculate(self):
# text = self.lineEdit.text()
# self.lineEdit.setText('%s= %.2f' % (text, eval(text)))
text = self.lineEdit.text()
self.textBrowser.append('%s= %.2f' % (text, eval(text)))
self.lineEdit_clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = mwindow()
w.pushButton.clicked.connect(w.ps_bt)
w.pushButton_1.clicked.connect(w.ps_bt1)
w.pushButton_2.clicked.connect(w.ps_bt2)
w.pushButton_3.clicked.connect(w.ps_bt3)
w.pushButton_4.clicked.connect(w.ps_bt4)
w.pushButton_5.clicked.connect(w.calculate)
w.pushButton_6.clicked.connect(w.ps_bt6)
w.pushButton_7.clicked.connect(w.ps_bt7)
w.pushButton_8.clicked.connect(w.ps_bt8)
w.pushButton_9.clicked.connect(w.ps_bt9)
w.pushButton_10.clicked.connect(w.ps_bt10)
w.pushButton_11.clicked.connect(w.ps_bt11)
w.pushButton_12.clicked.connect(w.ps_bt12)
w.pushButton_13.clicked.connect(w.ps_bt13)
w.pushButton_14.clicked.connect(w.ps_bt14)
w.pushButton_15.clicked.connect(w.ps_bt15)
w.pushButton_16.clicked.connect(w.ps_bt16)
w.pushButton_17.clicked.connect(w.ps_bt17)
w.pushButton_18.clicked.connect(w.ps_bt18)
w.pushButton_19.clicked.connect(w.ps_bt19)
w.show()
sys.exit(app.exec_())
运行Run.py,可在里面进行数值计算:
另外,如果想加入界面背景,并且自适应界面的大小,可以在主文件里面这么写:
def resizeEvent(self, event):
palette = QPalette()
pix = QPixmap('resources/background.jpg')
pix = pix.scaled(self.width(), self.height())
palette.setBrush(QPalette.Background, QBrush(pix))
self.setPalette(palette)
进阶版链接地址:
pyqt5与qtdesigner结合开发_pyqt5qtdesigner,pyqt5qt-Python代码类资源-CSDN下载
我曾经跨过山和大海,也穿过人山人海,我曾经拥有着的一切,转眼都飘散如烟,我曾经失落失望失掉所有方向,直到看见平凡才是唯一的答案。
——韩寒《平凡之路》
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/7484.html