【PyQt5-Qt Designer】猜数字(小项目)
参考:https://zhuanlan.zhihu.com/p/28287825
https://www.cnblogs.com/hhh5460/p/5174266.html

最终效果:

本次练习涉及的知识点,主要是以下几点:
按钮小提示
QLineEdit小部件使用
QMessageBox的使用
关闭窗口事件触发
本案例使用Qt 设计师完成,使用pyqt5+python3 版本
方式一:
逻辑和界面分离思想
界面代码:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setEnabled(True)
Dialog.resize(400,280)
# Dialog.setStyleSheet("image: url(:/图片/05.jpg);")
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
font = QtGui.QFont()
font.setPointSize(17)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setStyleSheet("")
self.lineEdit.setFrame(True)
self.lineEdit.setDragEnabled(False)
self.lineEdit.setReadOnly(False)
self.lineEdit.setCursorMoveStyle(QtCore.Qt.VisualMoveStyle)
self.lineEdit.setClearButtonEnabled(False)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 1, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem1, 5, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(20, 10, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem2, 2, 1, 1, 1)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem3, 1, 0, 1, 1)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem4, 1, 2, 1, 1)
self.pushButton = QtWidgets.QPushButton(Dialog)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(18)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setLayoutDirection(QtCore.Qt.LeftToRight)
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("font: 18pt \"黑体\";\n"
"color: rgb(255, 0, 127);")
self.pushButton.setDefault(False)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 3, 1, 1, 1)
spacerItem5 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem5, 3, 0, 1, 1)
spacerItem6 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem6, 3, 2, 1, 1) self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "猜数字游戏"))
self.lineEdit.setText(_translate("Dialog", "请输入数字"))
self.pushButton.setText(_translate("Dialog", "我猜")) import pictures_rc
界面代码
逻辑代码:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# from Ui_猜数字界面 import Ui_Dialog
import random class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(Qt.Window) # 设置窗口最大化最小化
self.answer = random.randint(0,100) #获得0,100之间的随机整数
print(self.answer) @pyqtSlot()
def on_pushButton_clicked(self):
you_guess = self.lineEdit.text()
if int(you_guess) > self.answer:
QMessageBox.information(self,
"消息框标题",
"你猜的数大了",
QMessageBox.Yes | QMessageBox.No)
elif int(you_guess) < self.answer:
QMessageBox.information(self,
"消息框标题",
"你猜的数小了",
QMessageBox.Yes | QMessageBox.No)
else:
QMessageBox.information(self,
"消息框标题",
"恭喜你猜对了!",
QMessageBox.Yes | QMessageBox.No)
time.sleep(0.3)
self.close() if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())
方式二:
最终项目提交时可以把 界面和逻辑放在一个py文件
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import random
import pictures_rc class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setEnabled(True)
Dialog.resize(400,280)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
font = QtGui.QFont()
font.setPointSize(17)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setStyleSheet("")
self.lineEdit.setFrame(True)
self.lineEdit.setDragEnabled(False)
self.lineEdit.setReadOnly(False)
self.lineEdit.setCursorMoveStyle(QtCore.Qt.VisualMoveStyle)
self.lineEdit.setClearButtonEnabled(False)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 1, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem1, 5, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(20, 10, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem2, 2, 1, 1, 1)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem3, 1, 0, 1, 1)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem4, 1, 2, 1, 1)
self.pushButton = QtWidgets.QPushButton(Dialog)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(18)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setLayoutDirection(QtCore.Qt.LeftToRight)
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("font: 18pt \"黑体\";\n"
"color: rgb(255, 0, 127);")
self.pushButton.setDefault(False)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 3, 1, 1, 1)
spacerItem5 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem5, 3, 0, 1, 1)
spacerItem6 = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem6, 3, 2, 1, 1) self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "猜数字游戏"))
self.lineEdit.setText(_translate("Dialog", "请输入数字"))
self.pushButton.setText(_translate("Dialog", "我猜")) class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(Qt.Window) # 设置窗口最大化最小化
self.answer = random.randint(0,100) #获得0,100之间的随机整数
print(self.answer) @pyqtSlot()
def on_pushButton_clicked(self):
you_guess = self.lineEdit.text()
if int(you_guess) > self.answer:
QMessageBox.information(self,
"消息框标题",
"你猜的数大了",
QMessageBox.Yes | QMessageBox.No)
elif int(you_guess) < self.answer:
QMessageBox.information(self,
"消息框标题",
"你猜的数小了",
QMessageBox.Yes | QMessageBox.No)
else:
QMessageBox.information(self,
"消息框标题",
"恭喜你猜对了!",
QMessageBox.Yes | QMessageBox.No)
time.sleep(0.3)
self.close() if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())
【PyQt5-Qt Designer】猜数字(小项目)的更多相关文章
- Java基础知识强化之IO流笔记70:Properties练习之 如何让猜数字小游戏只能玩5次的案例
1. 使用Properties完成猜数字小游戏只能玩5次的案例: 2. 代码实现: (1)猜数字游戏GuessNumber: package cn.itcast_08; import java.uti ...
- java猜数字小游戏
/* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...
- 简单的C语言猜数字小游戏
猜数字小游戏可谓是C语言最为基础的一个知识点了,我们可以在此基础上进行延伸,实现随机数的猜测,然后是加入再来一局的模式,等等.这里是抛砖引玉,希望你能做出你的经典之作. #include <st ...
- [Python3 练习] 007 简单的猜数字小游戏
题目:简单的猜数字小游戏 (1) 描述 程序随机生成一个数字,玩家用键盘输入所猜数字,在规定次数内猜对为胜. (2) 要求 程序随机生成一个 1 到 100 的自然数 有 7 次机会去猜 机会用尽之前 ...
- 简单的猜数字小游戏--Python
猜数字小游戏: #coding=utf-8 import random answer =random.randint(1,100) #生成随机数 n=int (input("Please ...
- PyQt5 & Qt Designer使用小结
开始在知乎写文章的原因,主要还是想整理平时的经验,方便自己以后查看,有机会的话大家也可以交流吧. 11月中旬由于项目需要,和另一名实习生负责使用Python开发一个数据分析的小软件. 虽然才开始接触Q ...
- Demo_2:Qt实现猜字小游戏
1 环境 系统:windows 10 代码编写运行环境:Qt Creator 4.4.1 (community) Github: 2 简介 参考视频:https://www.bilibili.co ...
- PyCharm+PyQt5+Qt Designer配置
配置前提 因为本机已经配置完毕了,本次使用的是虚拟机中的Win7系统,Win10系统操作步骤完全一样,无任何区别 PyCharm (这个不多说,官网下载安装,我是用的是2019.3版本) Python ...
- 零基础自学Python十天,写了一款猜数字小游戏,附源码和软件下载链接!
自学一门语言最重要的是要及时给自己反馈,那么经常写一些小程序培养语感很重要,写完可以总结一下程序中运用到了哪些零散的知识点. 本程序中运用到的知识点有: 1.输入输出函数 (input.print) ...
- Java(控制台输出)实现猜数字小游戏
import java.util.Scanner; import java.util.Random; public class GuestNum{ public static void main(St ...
随机推荐
- 《FPGA全程进阶---实战演练》第七章 让按键恢复平静
1基础理论部分 A:“怎么按键按下去之后,结果不正常?”,B:“按键你消抖了吗?”A:“消什么抖,还要消抖?”, B:“先检测按键变化,然后消抖过滤波动信号,最后输出稳定信号”,A:“我好像漏掉了什 ...
- (原)Android在子线程用handler发送的消息,主线程是怎么loop到的?
来自知乎:https://www.zhihu.com/question/48130951?sort=created 大家都知道Android的Looper是ThreadLocal方式实现,每个线程 ...
- [Object Tracking] Overview of algorithms for Object Tracking
From: https://www.zhihu.com/question/26493945 可以载入史册的知乎贴 目标跟踪之NIUBILITY的相关滤波 - 专注于分享目标跟踪中非常高效快速的相关滤波 ...
- 条件变脸pthread_cond_signal丢失问题
直接上代码: static bsem_t bsem; void* t1(void *arg) { /*printf("enter task 1\n");*/ /*while(1)* ...
- POJ 3249 Test for Job
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13457 Accepted: 3100 Descrip ...
- perl 遍历指定目录下的所有文件,替换指定文本内容,返回受影响的文件路径
不会读取 影藏文件 main #!/usr/bin/perl use autodie; use utf8; use Encode qw(decode encode); if(@ARGV ne 3){ ...
- Invalid file name: must contain only [a-z0-9_.]【Android报错】
Invalid file name: must contain only [a-z0-9_.][Android报错] 如: `[2012-02-07 09:58:14 - EmergencyRespo ...
- 7个简单的Excel技巧,需要的赶紧get起来吧
1.直观数据图形化 2. Ctrl不连续选择 3. Ctrl+A相连文本框全选 4. 格式刷点击 5. SUM函数求和 6. 自动求和.自动求平均值.自动计数 7. 行.列距调节
- myEclipse异常:Subversion Native Library Not Available
参考文档:https://blog.csdn.net/zp357252539/article/details/44880319 Subversion Native Library Not Availa ...
- js模拟浏览器加载效果 pace.js 中文官方文档
2017年2月20日12:11:25 官网URL:http://github.hubspot.com/pace/docs/welcome/ 文档 http://github.hubspot.com/p ...