In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use Python to do a class roll call", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python to do a class roll call" this article.
First of all, let's take a look at the effect of the roll call:
Next, we begin to introduce the production process of this roll call.
To get the source code and exe package, click to download
I. Design of core functions
Generally speaking, the idea of our roll call device is that we can customize the names of students in the class or use the student number to select students randomly by default, and we can choose the number of students to be selected for each time, and display the historical data of the names of these randomly selected students.
To disassemble the requirements, we can implement them in the following steps:
UI typesetting layout design, confirm each function design of roll call
Read the name file of the class students, load each student's name into the list, and create a student number file by default if there is no student name file.
Realize the function of random roll call and extraction of students' names.
Realize the mode of multi-person continuous pumping
View randomly selected student name history data
II. Implementation steps
Before, some readers said that they didn't know which modules and package files were needed in the program, so I put the modules used in the program out first. It mainly includes file reading and writing, random number, PyQt5,win32con and so on.
Import sys,osfrom random import randintfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtGui import * from PyQt5.QtCore import Qt, QTimerfrom PyQt5.QtWidgets import * from win32api import MessageBoxfrom win32con import MB_OK, MB_ICONWARNING1. UI typesetting layout Design
According to the functions required by the roll call, we can first design the UI layout, and we still use pyqt5 this time. It mainly includes the function of random extraction and display of names, the display of random extraction patterns, the function of viewing the historical results of extraction, the reading and display of name files, and so on. The core design code is as follows:
# author:Dragon Junior def setupUi (self, MainWindow): # paste the generated setupui code MainWindow.setObjectName ("Roller") MainWindow.resize (420,360) self.centralwidget = QtWidgets.QWidget (MainWindow) self.centralwidget.setObjectName ("centralwidget") self.label = QtWidgets.QLabel (self.centralwidget) self.label.setGeometry (QtCore.QRect (55,50,331) 71) font = QtGui.QFont () font.setFamily ("Verdana") font.setPointSize (50) self.label.setFont (font) self.label.setObjectName ("label") self.pushButton = QtWidgets.QPushButton (self.centralwidget) self.pushButton.setGeometry (QtCore.QRect (55,190,111) 61) font = QtGui.QFont () font.setFamily ("Verdana") font.setPointSize (20) self.pushButton.setFont (font) self.pushButton.setObjectName ("pushButton") self.pushButton_2 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_2.setGeometry (QtCore.QRect (253,190,111) 61) font = QtGui.QFont () font.setFamily ("Verdana") font.setPointSize (20) self.pushButton_2.setFont (font) self.pushButton_2.setObjectName ("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_3.setGeometry (QtCore.QRect (11,570,111) 41) self.pushButton_3.setObjectName ("pushButton_3") self.pushButton_4 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_4.setGeometry (QtCore.QRect (0830,111,41) self.pushButton_4.setObjectName ("pushButton_4") self.listWidget = QtWidgets.QListWidget (self.centralwidget) self.listWidget.setGeometry (QtCore.QRect (11,370,397) ) self.listWidget.setObjectName ("listWidget") self.label_2 = QtWidgets.QLabel (self.centralwidget) self.label_2.setGeometry (QtCore.QRect (11,340,210,21) self.label_2.setObjectName ("label_2") self.pushButton_5 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_5.setGeometry (QtCore.QRect (11303,111) 20) self.pushButton_5.setObjectName ("pushButton_5") self.pushButton_6 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_6.setGeometry (QtCore.QRect (319,300,75,20) self.pushButton_6.setObjectName ("pushButton_6") self.label_3 = QtWidgets.QLabel (self.centralwidget) self.label_3.setGeometry (QtCore.QRect (495,260,56) 21) self.label_3.setObjectName ("label_3") self.label_3.setStyleSheet ('color:white Background:#222225') self.pushButton_7 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_7.setGeometry (QtCore.QRect (649,240111) 61) font = QtGui.QFont () font.setFamily ("Verdana") font.setPointSize (30) self.pushButton_7.setFont (font) self.pushButton_7.setObjectName ("pushButton_7") self.listWidget_2 = QtWidgets.QListWidget (self.centralwidget) self.listWidget_2.setGeometry (QtCore.QRect (473,20,353) Font = QtGui.QFont () font.setPointSize (14) self.listWidget_2.setFont (font) self.listWidget_2.setFocusPolicy (QtCore.Qt.WheelFocus) self.listWidget_2.setVerticalScrollBarPolicy (QtCore.Qt.ScrollBarAsNeeded) self.listWidget_2.setHorizontalScrollBarPolicy (QtCore.Qt.ScrollBarAsNeeded) self.listWidget_2.setSizeAdjustPolicy (QtWidgets.QAbstractScrollArea.AdjustToContents) self.listWidget_2.setObjectName ("listWidget_2") MainWindow.setCentralWidget ( Self.centralwidget) self.menubar = QtWidgets.QMenuBar (MainWindow) self.menubar.setGeometry (QtCore.QRect (0) 0, 874 23) self.menubar.setObjectName ("menubar") MainWindow.setMenuBar (self.menubar) self.statusbar = QtWidgets.QStatusBar (MainWindow) self.statusbar.setObjectName ("statusbar") MainWindow.setStatusBar (self.statusbar) self.retranslateUi (MainWindow) QtCore.QMetaObject.connectSlotsByName (MainWindow) self.pushButton.clicked.connect (self.start) self.pushButton_2.clicked.connect (self.stop) self.pushButton_5.clicked.connect (self .showHistory) self.pushButton_6.clicked.connect (self.showContinue) self.pushButton_7.clicked.connect (self.ten) self.listWidget.setStyleSheet (self.scc) self.listWidget_2.setStyleSheet (self.scc) MainWindow.setWindowOpacity (.95) # set window transparency MainWindow.setAttribute (Qt.WA_TranslucentBackground) MainWindow.setWindowFlag (Qt.FramelessWindowHint) # hide the border Self.pushButton_8 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_8.setGeometry (QtCore.QRect 570,100,41) self.pushButton_8.setObjectName ("pushButton_8") self.pushButton_8.clicked.connect (self.rename) self.pushButton_8.setText ('reset name file') # author:Dragon Juvenile def retranslateUi (self, MainWindow): self.wide = 420 self.high = 360 _ translate = QtCore.QCoreApplication.translate _ translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle (_ translate ("MainWindow") "MainWindow") self.label.setText (_ translate ("MainWindow", "Congratulations {}") self.label.setStyleSheet ('color:white') self.pushButton.setText (_ translate ("MainWindow", "start")) self.pushButton_2.setText (_ translate ("MainWindow", "end")) self.pushButton_3.setText (_ translate ("MainWindow") "Open name file") self.pushButton_4.setText (_ translate ("MainWindow", "open gua option") self.label_2.setText (_ translate ("MainWindow", "student ID / name:")) self.pushButton_5.setText (_ translate ("MainWindow", "view the name")) self.pushButton_6.setText (_ translate ("MainWindow") "continuous pumping mode") self.label_3.setText (_ translate ("MainWindow", "continuous pumping") self.pushButton_7.setText (_ translate ("MainWindow", "start"))
The implementation effect of UI is as follows:
For this roll call interface, some readers and fans privately trusted me before, how to show and hide some content dynamic control, today we will use this roll call to demonstrate the implementation.
For example, for this roll call, we want to make the interface of the roll call more concise and intuitive, instead of displaying the extracted student history data or related content by default, as shown in the following figure.
So how to achieve this function? In fact, it is also very simple, as long as we define an event function, by resize reset the width and height of the interface form, and then bind the event function to similar button click events, we can dynamically show / hide part of the interface content. The core code is as follows:
# author:Dragon teenager # call this function when you click "View the clicked name" Resize reset the height size of the interface form def showHistory (self): global seed if not seed: self.high = 656 MainWindow.resize (self.wide,self.high) seed = True else: self.high = 360 MainWindow.resize (self.wide,self.high) seed = False#, call this function when you click "continuous pumping mode" Resize reset the width of the interface form def showContinue (self): global choud if not choud: self.wide = 874 MainWindow.resize (self.wide, self.high) choud = True else: self.wide = 420 MainWindow.resize (self.wide, self.high) choud = False
UI layout design is complete, let's start to read and load the names of the class students.
two。 Student name loading
Next, we need to load the names of the students to be selected randomly and put them into a list. Here, we need to pay attention to whether there is a name.txt file. If there is any file that can be read and loaded directly, if there is no such file, then the student ID file needs to be created by default according to the number. The core code is as follows:
# author:Dragon teenager def name (): with open ('name.txt', 'w') as f: print (f.truncate ()) print (f.write (a)) try: wordlist3 = [] with open (' name.txt' Encoding='utf8') as f: for line in f.readlines (): wordlist3.append (line.strip ('\ n')) # strip ('\ n') remove'\ n' print (wordlist3) name_list = wordlist3except: name () MessageBox (0, "modify the name file in the current directory in time The default will be 1-52 "," MessageBox ", MB_OK | MB_ICONWARNING) name_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19' '20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43 44, 45, 46, 47, 48, 49, 50, 51, 52,] 3. Realization of random roll call
Random roll call we can design two buttons to control the start of extraction and stop extraction function, through the timer to randomly extract student name list data, so as to achieve the function of random roll call. The core code is as follows:
# author:Dragon teenager # randomly display the list of names to simulate random roll call function def setname (self): global running global name try: name = name_ list [Randint (0, len (name_list)-1)] self.label.setText ("Congratulations {}!" .format (name) except: self.name () reply = QtWidgets.QMessageBox.warning (self, u 'warning', u 'error occurred Please check the student number of the name file before reopening the software' QtWidgets.QMessageBox.Yes) sys.exit () # start button binding load random roll call function def start (self) through timer: global running if running: print ('running') pass else: self.timer = QTimer (self) self.timer.timeout.connect (self.setname) self.timer.start (50) running =' True'# End button binding controls timer stop def stop (self): global running An if running: self.timer.stop () running = False self.listWidget.addItem (name) else: reply = QtWidgets.QMessageBox.warning (self, u 'warning', u 'want to finish before it starts?' , QtWidgets.QMessageBox.Yes)
At this point, we can implement the roll call function, as shown below.
4. Realization of continuous pumping mode
The function of random extraction has been realized, and multi-person continuous sampling is similar to random sampling, as long as the corresponding number of people are randomly selected from the name list. Of course, for the number of people to be extracted, we need to impose some input restrictions to avoid some abnormal input situations, such as non-numeric input, negative input, excessive input, and so on. All we have to do is bind the function to the button as well. The core code is as follows:
# author:Dragon teenager def ten (self): num = self.lineEdit.text () print (num) num = int (num) if not num =''and not num1000: if num > 20: reply = QtWidgets.QMessageBox.warning (self, u' warning', u 'serious? So many', QtWidgets.QMessageBox.Yes) self.listWidget_2.clear () for i in range (0 name (num)): name = name_ [Randint (0, len (name_list)-1)] self.listWidget_2.addItem (name) self.listWidget.addItem (name) elif num = ='': reply = QtWidgets.QMessageBox.warning (self, u' warning') U 'Please enter a number', QtWidgets.QMessageBox.Yes) self.listWidget_2.clear () elif num1000: # win32api.MessageBox (0, "so big? Cannot afford ~ "," notice ", win32con.MB_OK | win32con.MB_ICONWARNING) reply = QtWidgets.QMessageBox.warning (self, u 'warning', u 'the number exceeds the limit, incorrect input!' , QtWidgets.QMessageBox.Yes) self.listWidget_2.clear () 5. Extract history view
Finally, we implement a function of displaying and viewing the historical data of the names of the students who are called. The implementation of this function is also very simple, as long as the names randomly selected above are added to the corresponding history list box; you can also reset the student name file and other functions. The core code is as follows:
# author:Dragon Juvenile def rename (self): reply = QtWidgets.QMessageBox.question (self, u' warning', u' make sure the name file is reset to 1-52 'warning, QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: with open (' name.txt' 'w') as f: print (f.truncate ()) print (f.write (a)) MessageBox (0, "reset complete,", "Notification", MB_OK | MB_ICONWARNING) else: pass
At this point, this roll call is complete, let's take a look at the effect!
4. Packaged configuration
Recently, several fans asked me if Python could be packaged as exe. How do I pack it? It has been introduced before, and today we will introduce it again through this.
The commonly used packaging tool for Python is the third-party library Pyinstaller, which needs to be installed under pyinstaller.
Pip install Pyinstaller
Next, we need to open a command window, change to the project directory and execute the packaging command.
Pyinstaller-F-I. / img.ico roll call. Py
The common parameters for packaging are as follows:
-F means to generate a single executable file
-p means that you customize the classpath that needs to be loaded, which is not needed in general.
-I represents the icon of the executable file
-w means to remove the console window, which is very useful in the GUI interface
A friend asked me how to convert an ordinary picture to an icon format .ico if the packaged icon needs a .ico suffix. Python can also help you transfer pictures to .ico files. The core code is as follows:
Import PythonMagick# generates icon ico (png format picture to ico) img = PythonMagick.Image ('. / image/img1/1.png') # set the size here, otherwise it will report an abnormal ico size error img.sample ('128x128') img.write ('. / img.ico') the above is all the contents of the article "how to use Python to make a class roll call". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.