本帖最后由 fsjsyg 于 2024-06-22 15:05 编辑
广东省车牌问答小程序源码创作缘由近期被短视频的山东车牌洗脑后,觉得有必要记一下广东省内的车牌号码来自于哪个城市。所以就出现了这个小玩意。 程序玩法windows双击打开后,根据问题输入对应的答案,回车即可提交答案,答对了会自动下一题,答错了会显示答案。底部会显示正确率和答题数。 题目是随机出现的,也可以手动切换下一题 程序截图![](/images/article/1b7da84b-b3e8-4b6a-8a97-477315cb71f2.png)
程序代码import sys import random from collections import deque from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton from PyQt5.QtGui import QFont from PyQt5.QtCore import Qt, QTimer
class GuangdongPlateGame(QWidget): def __init__(self): super().__init__() self.initUI() self.plates = { "粤A": "广州", "粤B": "深圳", "粤C": "珠海", "粤D": "汕头", "粤E": "佛山", "粤F": "韶关", "粤G": "湛江", "粤H": "肇庆", "粤J": "江门", "粤K": "茂名", "粤L": "惠州", "粤M": "梅州", "粤N": "汕尾", "粤P": "河源", "粤Q": "阳江", "粤R": "清远", "粤S": "东莞", "粤T": "中山", "粤U": "潮州", "粤V": "揭阳", "粤W": "云浮", "粤X": "顺德", "粤Y": "南海", "粤Z": "港澳", "粤O": "机关单位" } self.current_plate = "" self.timer = QTimer(self) self.timer.setSingleShot(True) self.timer.timeout.connect(self.next_question) self.total_questions = 0 self.correct_answers = 0 self.wrong_answers = 0 self.recent_plates = deque(maxlen=3) # 用于存储最近3次出现的车牌 self.next_question()
def initUI(self): layout = QVBoxLayout()
self.question_label = QLabel() self.question_label.setAlignment(Qt.AlignCenter) self.question_label.setFont(QFont('Arial', 24)) layout.addWidget(self.question_label)
self.answer_input = QLineEdit() self.answer_input.setFont(QFont('Arial', 18)) self.answer_input.returnPressed.connect(self.check_answer) layout.addWidget(self.answer_input)
self.result_label = QLabel() self.result_label.setAlignment(Qt.AlignCenter) self.result_label.setFont(QFont('Arial', 16)) layout.addWidget(self.result_label)
self.stats_label = QLabel() self.stats_label.setAlignment(Qt.AlignCenter) self.stats_label.setFont(QFont('Arial', 14)) layout.addWidget(self.stats_label)
self.next_button = QPushButton('下一题') self.next_button.clicked.connect(self.next_question) layout.addWidget(self.next_button)
self.setLayout(layout) self.setGeometry(300, 300, 400, 300) self.setWindowTitle('广东车牌游戏') self.show()
def next_question(self): available_plates = [plate for plate in self.plates.keys() if plate not in self.recent_plates] if not available_plates: available_plates = list(self.plates.keys())
self.current_plate = random.choice(available_plates) self.recent_plates.append(self.current_plate)
self.question_label.setText(f"请输入 {self.current_plate} 对应的地区:") self.answer_input.clear() self.result_label.clear() self.answer_input.setEnabled(True) self.answer_input.setFocus() self.update_stats()
def check_answer(self): user_answer = self.answer_input.text().strip() correct_answer = self.plates[self.current_plate] self.total_questions += 1 if user_answer == correct_answer: self.result_label.setText("答对了!") self.correct_answers += 1 self.answer_input.setEnabled(False) self.timer.start(1000) else: self.result_label.setText(f"答错了。正确答案是:{correct_answer}") self.wrong_answers += 1 self.update_stats()
def update_stats(self): accuracy = (self.correct_answers / self.total_questions * 100) if self.total_questions > 0 else 0 stats_text = f"总题数: {self.total_questions} | 正确: {self.correct_answers} | 错误: {self.wrong_answers} | 正确率: {accuracy:.2f}%" self.stats_label.setText(stats_text)
if __name__ == '__main__': app = QApplication(sys.argv) ex = GuangdongPlateGame() sys.exit(app.exec_())
源码编译步骤- Python确认安装了PyQt5和PyInstaller
pip install PyQt5 pyinstaller
2.进行编译操作 pyinstaller --onefile --windowed guangdong_plate_game.py
附件懒人直接下载链接:https://wwk.lanzouj.com/io34622e0uti
注:若转载请注明大神论坛来源(本贴地址)与作者信息。
|