圆月山庄资源网 Design By www.vgjia.com
本文实例为大家分享了pycharm实现猜数游戏的具体代码,供大家参考,具体内容如下
1. 设计界面
如下所示,利用QTdesigner设计的界面:
然后将设计好的界面转换为.py文件:
guess_number.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'guess_number.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(580, 328) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Normal, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Disabled, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Disabled, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Active, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Active, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Selected, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("../../../Icon/Comment.svg"), QtGui.QIcon.Selected, QtGui.QIcon.On) MainWindow.setWindowIcon(icon) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout_4 = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout_4.setObjectName("gridLayout_4") spacerItem = QtWidgets.QSpacerItem(20, 50, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_4.addItem(spacerItem, 0, 1, 1, 1) spacerItem1 = QtWidgets.QSpacerItem(181, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_4.addItem(spacerItem1, 1, 3, 1, 1) spacerItem2 = QtWidgets.QSpacerItem(180, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_4.addItem(spacerItem2, 1, 0, 1, 1) self.gridLayout_3 = QtWidgets.QGridLayout() self.gridLayout_3.setObjectName("gridLayout_3") spacerItem3 = QtWidgets.QSpacerItem(17, 37, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_3.addItem(spacerItem3, 0, 2, 1, 1) self.gridLayout_2 = QtWidgets.QGridLayout() self.gridLayout_2.setObjectName("gridLayout_2") self.button = QtWidgets.QPushButton(self.centralwidget) self.button.setObjectName("button") self.gridLayout_2.addWidget(self.button, 2, 1, 1, 1) self.inputnumber = QtWidgets.QLineEdit(self.centralwidget) self.inputnumber.setObjectName("inputnumber") self.gridLayout_2.addWidget(self.inputnumber, 0, 0, 1, 3) spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_2.addItem(spacerItem4, 2, 2, 1, 1) spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_2.addItem(spacerItem5, 2, 0, 1, 1) spacerItem6 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_2.addItem(spacerItem6, 1, 1, 1, 1) self.gridLayout_3.addLayout(self.gridLayout_2, 1, 1, 2, 2) spacerItem7 = QtWidgets.QSpacerItem(37, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem7, 1, 3, 1, 1) spacerItem8 = QtWidgets.QSpacerItem(37, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem8, 1, 0, 1, 1) spacerItem9 = QtWidgets.QSpacerItem(17, 37, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_3.addItem(spacerItem9, 3, 1, 1, 1) self.gridLayout_4.addLayout(self.gridLayout_3, 1, 1, 1, 2) spacerItem10 = QtWidgets.QSpacerItem(20, 49, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_4.addItem(spacerItem10, 2, 2, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 580, 28)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "猜数游戏")) self.button.setText(_translate("MainWindow", "我猜")) self.inputnumber.setText(_translate("MainWindow", "在这里输入数字"))
2.对ui界面的功能具体实现
如果直接使用生成好的.py文件使用起来不是很方便,修改界面以后重新生成的.py文件会直接覆盖,这里我采用一个新类直接获取前面设计好的界面从而实现算法与界面程序分离,先上代码。
main.py
import sys import guess_number from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QWidget from random import randint class guess_ui(QMainWindow): def __init__(self): # get ui from guess_number super().__init__() self.num = randint(1, 100) self.MainWindow = QMainWindow() self.InitUI() def InitUI(self): ui = guess_number.Ui_MainWindow() ui.setupUi(self) button = self.findChild(QWidget, 'button') button.clicked.connect(self.show_message) self.show() # self.MainWindow.show() def show_message(self): inputnumber = self.findChild(QWidget, 'inputnumber') guessnumber = int(inputnumber.text()) print(guessnumber) if guessnumber > self.num: QMessageBox.about(self, 'see the result', 'bigger!') inputnumber.setFocus() elif guessnumber < self.num: QMessageBox.about(self, 'see the result', 'smaller!') inputnumber.setFocus() else: QMessageBox.about(self, 'see the result', 'true') self.num = randint(1, 100) inputnumber.clear() inputnumber.setFocus() def closeEvent(self, event): reply = QMessageBox.question(self, 'confirm', 'Are you sure"htmlcode">class guess_ui(QMainWindow):将此类直接从QMainWindow继承过来可以实现对QT事件的重写,事件重现代码如下:
def closeEvent(self, event): reply = QMessageBox.question(self, 'confirm', 'Are you sure"htmlcode">button = self.findChild(QWidget, 'button')以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
pycharm,猜数
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 群星《戏梦》[FLAC/分轨][308.4MB]
- 魔兽世界永久60级什么时间开 永久60级开启时间介绍
- 魔兽世界奥卡兹岛血藻刷新点一览 wlk奥卡兹岛血藻刷新位置介绍
- 英雄联盟s14中国队种子怎么排名 s14中国队种子队伍排名一览
- 柏菲·梁玉嵘《知音梁品》限量开盘母带ORMCD[低速原抓WAV+CUE]
- [东升唱片]孙露《寂寞诱惑AQCD》高码率[FLAC]
- 群星-第8届2012十大发烧唱片精选HQ2CD[WAV+CUE]
- 九号公司两轮电动安全季行动圆满收官:为行业树立安全管理新标杆
- 创新驱动未来 中国移动5G云游戏深化智能运维应用推动行业发展
- 大型手游“帧数拉满” 骁龙8至尊版游戏表现强悍
- 谢金燕.2002-YOYO姊妹【华特】【WAV+CUE】
- 群星.1994-摇滚列车【音乐家】【WAV+CUE】
- 陈艾湄.1996-牵绊【巨翼】【WAV+CUE】
- 群星《无法抗拒的谎言 电视剧原声带》[320K/MP3][69.98MB]
- 群星《无法抗拒的谎言 电视剧原声带》[FLAC/分轨][372.04MB]