python+pyqt5+qt designer小程序

xiaoxiao2021-03-01  14

相关配置:

Anaconda 4.2.0 (64-bit) Python 3.5.2

qt designer设计界面如下:

转换成py文件后,csz.py程序如下:

# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'csz.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(386, 252) self.gridLayout = QtWidgets.QGridLayout(Form) self.gridLayout.setObjectName("gridLayout") self.textEdit = QtWidgets.QTextEdit(Form) self.textEdit.setObjectName("textEdit") self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1) self.label = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(18) self.label.setFont(font) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 1, 0, 1, 1) self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setObjectName("lineEdit") self.gridLayout.addWidget(self.lineEdit, 2, 0, 1, 1) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 3, 0, 1, 1) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "请在下面输入想要猜的数字:0-10")) self.pushButton.setText(_translate("Form", "我猜"))

新建一个run.py文件,程序如下:

# -*- coding: utf-8 -*- from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox from csz import Ui_Form import numpy as np import sys class mywindow(Ui_Form, QWidget): def __init__(self): super(mywindow, self).__init__() self.setupUi(self) self.num = np.random.randint(10) self.setWindowTitle('猜数字') print(self.num) def closeEvent(self, event): reply = QMessageBox.question(self, '确认', '确认退出吗?', QMessageBox.Yes|QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() def gass(self): number = int(self.lineEdit.text()) # if self.lineEdit.text().isEmpty(): # QMessageBox.about(self, '警告', '必须输入一个0-10的数字!') # self.lineEdit.clear() # self.lineEdit.setFocus() if number > self.num: QMessageBox.about(self, '结果', '猜大了!') self.textEdit.append(self.lineEdit.text()+' 猜大了!') self.lineEdit.clear() self.lineEdit.setFocus() elif number < self.num: QMessageBox.about(self, '结果', '猜小了!') self.textEdit.append(self.lineEdit.text()+' 猜小了!') self.lineEdit.clear() self.lineEdit.setFocus() elif number == self.num: QMessageBox.about(self, '结果', '恭喜你猜对了!') self.textEdit.append(self.lineEdit.text()) self.num = np.random.randint(10) print(self.num) self.lineEdit.clear() self.textEdit.clear() self.lineEdit.setFocus() else: pass if __name__ == '__main__': app = QApplication(sys.argv) w = mywindow() w.pushButton.clicked.connect(w.gass) w.show() sys.exit(app.exec_())

运行结果如下:

       我曾经跨过山和大海,也穿过人山人海,我曾经拥有着的一切,转眼都飘散如烟,我曾经失落失望失掉所有方向,直到看见平凡才是唯一的答案。 ——韩寒《平凡之路》

转载请注明原文地址: https://www.6miu.com/read-3850257.html

最新回复(0)