猜一猜
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QMessageBox,QLineEdit
from PyQt5.QtGui import QIcon
from random import randint
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.num=randint(1,10)
def initUI(self):
self.setGeometry(300,300,300,220)
self.setWindowTitle("早点毕业")
self.setWindowIcon(QIcon('x.png'))
self.bt=QPushButton('猜',self)
self.bt.setGeometry(115,150,70,30)
self.bt.setToolTip('点击猜数字')
self.bt.clicked.connect(self.showMessage)
self.text=QLineEdit('输入数字',self)
self.text.selectAll()
self.text.setFocus()
self.text.setGeometry(80,50,150,30)
self.show()
def showMessage(self):
guessnumber=int(self.text.text())
print(self.num)
if guessnumber>self.num:
QMessageBox.about(self,'看结果','大了')
self.text.setFocus()
elif guessnumber<self.num:
QMessageBox.about(self,'看结果','小了')
self.text.setFocus()
else:
QMessageBox.about(self,'看结果','ok,again')
self.num=randint(1,100)
self.text.clear()
self.text.setFocus()
def closeEvent(self,event):
reply=QMessageBox.question(self,'sure','quit?',QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
if reply==QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())