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

Relevant documents

Little buddy who wants to learn Python can pay attention to the official account of Xiaobian [Python journal].
There are many resources for whoring for nothing, ha. I will update the little knowledge of Python from time to time!!
Small buddy who needs source code can reply to guess number in official account.
Python source code, problem solving, learning exchange group: 773162165

code implementation

Import relevant modules of PyQt5

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

PyQt5 is installed by pip.

pip install PyQt5

Import the prepared style into the code block.

# Theme style module reference
from QCandyUi import CandyWindow

Files and random number related modules are imported.

import sys
import random

Interface programming function init_ui, initialize the UI interface and page layout code block.

def init_ui(self):
    self.setWindowTitle('Guess the number game official account:[Python journal]')
    self.setWindowIcon(QIcon('number.ico'))

    self.setFixedSize(500, 350)

    self.msg = QLabel()
    self.msg.setText('Figure guessing game')
    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('Please enter the target number')
    self.in_num.setAlignment(Qt.AlignCenter)

    self.in_num_btn = QPushButton()
    self.in_num_btn.setText('That's it')
    self.in_num_btn.clicked.connect(self.in_num_btn_click)

    self.tar_num_btn = QPushButton()
    self.tar_num_btn.setText('Display number')
    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('  Generate target number  ')
    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)

Slot function generate_num_btn_click,Used to generate the target number of the number guessing game.

    def generate_num_btn_click(self):
        tar_num = random.randint(1, 99)
        self.num = tar_num
        # Reset max min
        self.max_num = 100  # Current maximum
        self.min_num = 0  # Current minimum

Slot function tar_num_btn_click to show or hide the target number.

def tar_num_btn_click(self):
    if self.num != 0 and self.tar_num_btn.text().strip() == 'Display number':
        self.tar_num.setText(str(self.num))
        self.tar_num_btn.setText('Hide numbers')
    elif self.tar_num_btn.text().strip() == 'Hide numbers':
        self.tar_num.setText('#####')
        self.tar_num_btn.setText('Display number')

Slot function in_num_btn_click, which is used to deal with the main code logic of guessing numbers.

  def in_num_btn_click(self):
        try:
            in_num = int(self.in_num.text().strip())
            if in_num < self.min_num or in_num >= self.max_num:
                pass
            else:
                if self.num == 0:
                    self.msg.setText('No target number')
                elif in_num == self.num:
                    self.msg.setText('Congratulations, you guessed right')
                else:
                    if in_num < self.num:
                        self.msg.setText(str(in_num) + ' - ' + str(self.max_num))
                        self.min_num = in_num
                    elif in_num > self.num:
                        self.msg.setText(str(self.min_num) + ' - ' + str(in_num))
                        self.max_num = in_num
        except:
            self.msg.setText('please enter a number')

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

CandyWindow.createWindow(GuessNumber(), theme='blue', title='Guess the number game official account:[Python journal]',ico_path='number.ico')

Effect display

When starting the game, you must first click "generate target number" to start the game

If you want to cheat, you can click "show numbers" to see our answers

When I input the number, click the button "that's it"

When the number I input is the same as the number we generated, the game will end and display "Congratulations, you guessed right"

The case is just realized. If you have any problems, you can find Xiaobian for help

Little buddy who wants to learn Python can pay attention to the official account of Xiaobian [Python journal].
There are many resources for whoring for nothing, ha. I will update the little knowledge of Python from time to time!!
Small buddy who needs source code can reply to guess number in official account.
Python source code, problem solving, learning exchange group: 773162165

Keywords: Python Back-end

Added by xxxxxx on Fri, 04 Mar 2022 13:33:49 +0200