欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pycharm实现猜数游戏

程序员文章站 2022-04-03 15:50:17
本文实例为大家分享了pycharm实现猜数游戏的具体代码,供大家参考,具体内容如下1. 设计界面如下所示,利用qtdesigner设计的界面:然后将设计好的界面转换为.py文件:guess_numbe...

本文实例为大家分享了pycharm实现猜数游戏的具体代码,供大家参考,具体内容如下

1. 设计界面

如下所示,利用qtdesigner设计的界面:

pycharm实现猜数游戏

pycharm实现猜数游戏

然后将设计好的界面转换为.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?', qmessagebox.yes | qmessagebox.no)
  if reply == qmessagebox.yes:
   event.accept()
  else:
   event.ignore()


if __name__ == '__main__':
 app = qapplication(sys.argv)
 guess = guess_ui()
 sys.exit(app.exec_())

3.对部分代码的解析

class guess_ui(qmainwindow):

将此类直接从qmainwindow继承过来可以实现对qt事件的重写,事件重现代码如下:

def closeevent(self, event):
  reply = qmessagebox.question(self, 'confirm', 'are you sure?', qmessagebox.yes | qmessagebox.no)
  if reply == qmessagebox.yes:
   event.accept()
  else:
   event.ignore()

在界面文件中按钮的定义是直接add进去的,所以定义某个控件功能首先要获取该控件,代码如下所示:

button = self.findchild(qwidget, 'button')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: pycharm 猜数