GUI guessing number game, implemented with more than 100 lines of simple Python code

Related documents

Friends who need the original code and want to learn Python can ↓ ↓ ↓

Click here~~

There are a lot of resources that can be prostituted for nothing, and I will update the little knowledge of Python from time to time! !

Code

Import related modules of PyQt5

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

The installation of PyQt5 still uses the pip installation method.

pip install PyQt5

Import the prepared styles into the code block.

# 主题样式模块引用
from QCandyUi import CandyWindow

Files and modules related to random numbers are imported.

import sys
import random

The interface writing function init_ui initializes the UI interface and page layout code blocks.

def init_ui(self):
    self.setWindowTitle('猜数字游戏  公众号:[Python 日志]')
    self.setWindowIcon(QIcon('数字.ico'))

    self.setFixedSize(500, 350)

    self.msg = QLabel()
    self.msg.setText('猜数字游戏')
    self.msg.setStyleSheet(
        'font-size:50px;text-align:center;font-weight:bold;font-family:"Microsoft JhengHei";')
    self.msg.setAlignment(Qt.AlignCenter)

    self.in_num = QLineEdit()
    self.in_num.setPlaceholderText('请输入目标数字')
    self.in_num.setAlignment(Qt.AlignCenter)

    self.in_num_btn = QPushButton()
    self.in_num_btn.setText('就是它了')
    self.in_num_btn.clicked.connect(self.in_num_btn_click)

    self.tar_num_btn = QPushButton()
    self.tar_num_btn.setText('显示数字')
    self.tar_num_btn.clicked.connect(self.tar_num_btn_click)

    self.tar_num = QLabel()
    self.tar_num.setText('#####')
    self.tar_num.setFixedWidth(50)

    self.generate_num_btn = QPushButton()
    self.generate_num_btn.setText('  生成目标数字  ')
    self.generate_num_btn.clicked.connect(self.generate_num_btn_click)

    hbox = QHBoxLayout()
    hbox.addWidget(self.tar_num)
    hbox.addWidget(self.tar_num_btn)
    hbox.addStretch(1)
    hbox.addWidget(self.generate_num_btn)

    vbox = QVBoxLayout()
    vbox.addStretch(1)
    vbox.addWidget(self.msg)
    vbox.addWidget(self.in_num)
    vbox.addWidget(self.in_num_btn)
    vbox.addStretch(1)
    vbox.addLayout(hbox)

    self.setLayout(vbox)

槽函数 generate_num_btn_click,用于生成猜数字游戏的目标数字。

    def generate_num_btn_click(self):
        tar_num = random.randint(1, 99)
        self.num = tar_num
        # 重置最大最小值
        self.max_num = 100  # 当前最大值
        self.min_num = 0  # 当前最小值

The slot function tar_num_btn_click is used to show or hide the target number.

def tar_num_btn_click(self):
    if self.num != 0 and self.tar_num_btn.text().strip() == '显示数字':
        self.tar_num.setText(str(self.num))
        self.tar_num_btn.setText('隐藏数字')
    elif self.tar_num_btn.text().strip() == '隐藏数字':
        self.tar_num.setText('#####')
        self.tar_num_btn.setText('显示数字')

Finally, the whole app becomes beautiful by calling the blue theme style of the theme module.

CandyWindow.createWindow(GuessNumber(), theme='blue', title='猜数字游戏  公众号:[Python 日志]',ico_path='数字.ico')

Show results

When you start the game, you must first click "Generate target number" to start the game.
insert image description here
The case is implemented like this. If you have any questions, you can find the editor for help.

Friends who need the original code and want to learn Python can ↓ ↓ ↓

Click here~~

There are a lot of resources that can be prostituted for nothing, and I will update the little knowledge of Python from time to time! !

Guess you like

Origin blog.csdn.net/weixin_72934044/article/details/128136072