import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from random import randint

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.num = randint(1,100)
    def initUI(self):
        self.setGeometry(300,300,300,300)
        self.setWindowIcon(QIcon('./sources/favicon_star.png'))
        self.setWindowTitle('猜数字')

        self.bt1 = QPushButton('猜',self)
        self.bt1.setGeometry(115,150,70,30)
        self.bt1.setToolTip('<b>点击这里猜数字</b>')
        self.bt1.clicked.connect(self.showMessage)

        self.text = QLineEdit('写入数字',self)
        self.text.selectAll()
        self.text.setFocus()
        self.text.setGeometry(80,80,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, '看结果','答对了!进入下一轮!')
            self.num = randint(1,100)
            self.text.clear()
            self.text.setFocus()  
    def closeEvent(self,event):
        reply = QMessageBox.question(self,'确认','sure?',QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            event.accpet()
        else:
            event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())




