In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you how to use PyQt5 to make a number-guessing Mini Game with detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Before we begin, let's directly take a look at the effect of the implementation.
Import the relevant modules of PyQt5 directly.
From PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import *
In order to take care of the children's shoes of new concern, here is an introduction to the installation of PyQt5, or the installation of pip.
Pip install PyQt5
Import the prepared style into the code block.
# theme style module references from QCandyUi import CandyWindow
The modules related to files and random numbers are imported.
Import sysimport random
Interface writing function init_ui, initializing the UI interface and page layout code block.
Def init_ui (self): self.setWindowTitle ('guessing game official account: [Python concentration camp]') self.setWindowIcon (QIcon ('digital .ico')) self.setFixedSize (500,350) self.msg = QLabel () self.msg.setText ('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 ('this is 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, which is used to generate the target number of the guessing game.
Def generate_num_btn_click (self): tar_num = random.randint (1,99) self.num = tar_num # reset maximum and minimum self.max_num = 100 # current maximum self.min_num = 0 # current minimum
Slot function tar_num_btn_click, which 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 () = 'Show numbers': 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 ('Show numbers')
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 it') 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, the blue theme style of the theme module is called to make the whole App beautiful.
CandyWindow.createWindow (GuessNumber (), theme='blue', title=' guessing numbers official account: [Python concentration Camp]', ico_path=' numbers .ico')
Complete code
#-*-coding:utf-8-*-# @ author Python concentration camp # @ date 2022 Universe 2Universe @ file test1.py# done# GUI guessing game from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * # theme style module references from QCandyUi import CandyWindowimport sysimport randomclass GuessNumber (QWidget): def _ _ init__ (self): super (GuessNumber) Self). _ _ init__ () self.num = 0 # target number self.max_num = 100 # current maximum self.min_num = 0 # current minimum self.init_ui () def init_ui (self): self.setWindowTitle ('guessing game official account: [Python concentration camp]') self.setWindowIcon ( QIcon ('digital .ico') self.setFixedSize (500 Self.msg = QLabel () self.msg.setText ('guessing numbers') 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 ('this is 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) def generate_num_btn_click (self): tar_num = random.randint (1 99) self.num = tar_num # reset maximum and minimum self.max_num = 100 # current maximum self.min_num = 0 # current minimum 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 ('Hidden numbers') elif self.tar_num_btn.text () .strip () = 'Hidden numbers': self.tar_num.setText ('#') self.tar_num_btn.setText ('Show 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 it') 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') if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) w = CandyWindow.createWindow (GuessNumber (), theme='blue' Title=' guessing game official account: [Python concentration camp]', ico_path=' numbers .ico') w.show () sys.exit (app.exec_ ()) are all the contents of the article "how to use PyQt5 to make a guessing Mini Game" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.