#-*- coding:utf-8 -*- from PyQt5.QtWidgets import QApplication,QWidget,QTableWidgetItem import sys from form import Ui_Form import random import time class mywindow(QWidget,Ui_Form): def __init__(self): super().__init__() self.setupUi(self) self.btnstart.clicked.connect(self.start) self.btnok.clicked.connect(self.ok) self.btn0.clicked.connect(self.btn_0) self.btn1.clicked.connect(self.btn_1) self.btn2.clicked.connect(self.btn_2) self.btn3.clicked.connect(self.btn_3) self.btn4.clicked.connect(self.btn_4) self.btn5.clicked.connect(self.btn_5) self.btn6.clicked.connect(self.btn_6) self.btn7.clicked.connect(self.btn_7) self.btn8.clicked.connect(self.btn_8) self.btn9.clicked.connect(self.btn_9) self.btnreset.clicked.connect(self.reset) def start(self): self.groupBox.setEnabled(True) #解除按钮群组禁用状态 self.table.clearContents() self.label_2.setText(‘电脑自动生成0到100之间的随机数,请猜‘) self.x=random.randint(1,100) self.label.setText(‘‘) self.i=1 #计算步数 self.timea=time.time() #程序运行的起始时间 #0-9各按钮的点击 def btn_0(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘0‘) def btn_1(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘1‘) def btn_2(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘2‘) def btn_3(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘3‘) def btn_4(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘4‘) def btn_5(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘5‘) def btn_6(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘6‘) def btn_7(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘7‘) def btn_8(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘8‘) def btn_9(self): if self.label.text()==‘小了‘ or self.label.text()==‘大了‘: self.label.clear() self.i+=1 textt=self.label.text() self.label.setText(textt+‘9‘) def ok(self): x_text=int(self.label.text()) temp_x=QTableWidgetItem(self.label.text()) #表格内容必须先转换一下格式,才能插入表格 self.table.setItem(self.i-1,0,temp_x) #表格显示步数 if x_text == self.x: self.label.setText(‘恭喜过关‘) self.table.setItem(self.i-1,1,QTableWidgetItem(self.label.text())) #表格显示过关 timeb=time.time() timex=int(timeb-self.timea) self.label_2.setText(‘你一共用了{}秒,用了{}步‘.format(timex, self.i)) elif x_text<self.x: self.label.setText(‘小了‘) self.table.setItem(self.i-1,1,QTableWidgetItem(self.label.text())) #表格显示小了 else: self.label.setText(‘大了‘) self.table.setItem(self.i-1,1,QTableWidgetItem(self.label.text())) #表格显示大了 def reset(self): #重置按钮 text=self.label.text() self.label.setText(text[0:-1]) #原标签内容减去最后一个字母,再显示出来 if __name__==‘__main__‘: app=QApplication(sys.argv) w=mywindow() w.show() app.exec()
原文地址:https://www.cnblogs.com/jmlovepython/p/8446907.html
时间: 2024-10-09 11:47:45