猫史档案馆


【Python作品分享】24点,用Qt和Pyside2做的【作品秀】

用户:含蓄的雷电猴dkOl含蓄的雷电猴dkOl查看:0 回复:0 评论:0 创建时间:2022-07-02T13:56:35


【作品展示】

center_image

 

【作品介绍】

大家别复制本作品代码

因为本作品有很多附件

没有附件无法运行

看一下就行了

 

【作品源代码】

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from ui_24 import Ui_MainWindow
import random

class Game(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setup()
        self.show()

        self.sc = 0

        with open("best.txt", "r") as f:
            self.record_score = int(f.read())
        self.best.setText("best:" + str(self.record_score))

        self.pbtn_r.clicked.connect(self.setup)

    def setup(self):
        cards = [self.pbtn_1, self.pbtn_2, self.pbtn_3, self.pbtn_4]
        for card in cards:
            number = str(random.randint(1, 10))
            card.setText(number)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            print("提交")
            self.verify()

    def verify(self):
        answer = self.lineEdit.text()
        result = eval(answer)
        if result == 24 and answer != '24':
            print("答对了")
            self.score_display()
            self.setup()
        else:
            print("答错了")
        self.lineEdit.setText("")

    def score_display(self):
        self.sc += 5
        self.score.setText("score:" + str(self.sc))
        
        if self.sc > self.record_score:
            with open("best.txt", "w") as f:
                f.write(str(self.sc))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Game()
    sys.exit(app.exec_())

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页