回顾一下GUI编程流程,设计UI界面->设计逻辑功能->测试->打包。
今天先用一个小栗子说明这个过程,这次想做个猜数字的小游戏,程序自动生成一个随机的100以内的整数,然后用户输入猜测的数字,程序会提示猜大了还是猜小了,如果用二分法的话,7次以内应该能够猜中。
-
首先我们打开designer,设计我们的小游戏界面。如下图所示:
我们新建了一个QWidget作为程序外框,内部加入了一个空间容器QWidget,在此容器中加入了我们主要控件lineEdit和pushButton,并利用Spacers和Layout达到了我们希望的布局效果。保存ui文件。
利用pyuic将我们的ui文件转化为py文件,并在主文件中导入该文件。
这是ui文件转化为的py文件。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Guess_number.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# 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(476, 361)
self.verticalLayout_3 = QtWidgets.QVBoxLayout(Form)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.verticalLayout_2.setContentsMargins(-1, -1, -1, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.widget = QtWidgets.QWidget(Form)
self.widget.setObjectName("widget")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_4.addItem(spacerItem)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem1)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setInputMask("")
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem2)
self.verticalLayout_4.addLayout(self.horizontalLayout_2)
spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_4.addItem(spacerItem3)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem4)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem5)
self.verticalLayout_4.addLayout(self.horizontalLayout)
spacerItem6 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_4.addItem(spacerItem6)
self.verticalLayout_5.addLayout(self.verticalLayout_4)
self.verticalLayout_2.addWidget(self.widget)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "猜数字(100以内)"))
self.lineEdit.setText(_translate("Form", "在这里输入数字"))
self.pushButton.setToolTip(_translate("Form", "点击这里猜数字"))
self.pushButton.setText(_translate("Form", "我猜"))
接下来我们在主程序中引入该文件:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from Guess_number import Ui_Form # 引入ui转化的py文件
from random import randint # 随机数库
class Dialog(QWidget,Ui_Form): # 此处引用导入的ui
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.num = randint(1, 100)
self.lineEdit.selectAll() # 设置输入框全选
self.lineEdit.setFocus() # 设置焦点
self.pushButton.clicked.connect(self.showMessage) # 按钮连接至我们的逻辑函数
def showMessage(self):
guessnumber = self.lineEdit.text()
if guessnumber.isdigit():
guessnumber = int(self.lineEdit.text())
if guessnumber > self.num:
QMessageBox.about(self, '结果', '猜大了!')
self.lineEdit.selectAll()
self.lineEdit.setFocus()
elif guessnumber < self.num:
QMessageBox.about(self, '结果', '猜小了!')
self.lineEdit.selectAll()
self.lineEdit.setFocus()
else:
QMessageBox.about(self, '结果', '答对了!进入下一轮!')
self.num = randint(1, 100)
self.lineEdit.clear()
self.lineEdit.selectAll()
self.lineEdit.setFocus()
else:
self.input_error()
def closeEvent(self, event): # 重写关闭动作,弹出消息框以确认
reply = QMessageBox.question(self, '确认', '确认退出吗',QMessageBox.Yes |QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def keyPressEvent(self, event): # 将键盘输入enter绑定在按钮上
if (event.key() == Qt.Key_Enter):
self.pushButton.click()
if (event.key() == 16777220):
self.pushButton.click()
def input_error(self):
QMessageBox.critical(self, "错误", "输入错误请重新输入")
self.lineEdit.selectAll()
self.lineEdit.setFocus()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Dialog()
window.show()
sys.exit(app.exec_())
-
运行试试看
-
最后利用pyinstaller打包文件,生成exe,pyinstaller的简单使用大家可以去查阅一下之前的文章。生成的exe就在dist文件夹下。
按照这个流程就可以自己去做一些自己想做的程序了哈。
自己要学习的东西还有很多,和别人的差距还是很大,有点小苦恼哎~~~
不过时间是自己挤出来的,争取少玩一些游戏,少看些剧和综艺!
加油,晚安~