The same tiktok system named PyQt5 is very simple.

Posted by thebighere on Fri, 11 Feb 2022 12:29:39 +0100

Tiktok found a teacher's classroom roll call system. With PyQt5 to achieve the same paragraph, import the student name, test the perfect operation.

[read the full text]

Operation effect display:

The complete source code block is still placed at the end of the article. If you need to run directly to the end of the article to obtain the download method.

When using, prepare the file of students' names, and use the button to import data directly to start roll call. Create a new text document and set the name. The example of name file is as follows.

The use of system libraries or third-party libraries is relatively routine, so we won't introduce them one by one here.

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

from qdarkstyle import load_stylesheet_pyqt5

import os
import sys
import time
import random  

In order to separate from the main thread of the UI interface, we still use QThread multithreading to implement the random roll call module. The purpose of this is to prevent the main thread from blocking. The following is the implementation part of the sub thread.

class WorkThread(QThread):
    trigger = pyqtSignal(str)
    finished = pyqtSignal(bool)

    def __init__(self, parent=None):
        super(WorkThread, self).__init__(parent)
        self.parent = parent
        self.working = True

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        data_list = self.parent.data_list
        if len(data_list) >= 1:
            ran = random.randint(20, 40)
            print('Traversal times:', ran)
            for a in range(ran):
                name = random.choice(data_list)
                self.trigger.emit(name)
                print(name)
                time.sleep(0.6)
            self.finished.emit(True)
        else:
            self.trigger.emit('No data')

The implementation part of the UI interface is also relatively conventional. The following is the code block of the implementation part.

class ClassCollSystem(QWidget):
    def __init__(self):
        super(ClassCollSystem, self).__init__()
        self.data_list = []
        self.init_ui()

    def init_ui(self):
        '''Sub thread call'''
        self.thread_ = WorkThread(self)
        self.thread_.trigger.connect(self.set_name)
        self.thread_.finished.connect(self.finished)

        '''Application initialization information'''
        self.setWindowTitle('Official account of the classroom naming system:[Python concentration camp]')
        self.setWindowIcon(QIcon('Class roll call.ico'))
        self.setFixedSize(500, 350)

        '''Name information layout'''
        vbox_name = QVBoxLayout()
        self.current_name = QLabel()
        self.current_name.setText('Random roll call')
        self.current_name.setStyleSheet(
            'font-size:50px;text-align:center;font-weight:bold;font-family:"Microsoft JhengHei";')

        vbox_name.addWidget(self.current_name)
        vbox_name.setAlignment(Qt.AlignCenter)

        '''Start information layout'''
        vbox_start = QVBoxLayout()
        self.start_btn = QPushButton()
        self.start_btn.setText('Start roll call')
        self.start_btn.setFixedSize(160, 50)
        self.start_btn.setStyleSheet(
            'font-size:30px;font-weight:bold;text-align:center;font-family:"Microsoft JhengHei";')
        self.start_btn.clicked.connect(self.start_btn_click)

        vbox_start.addWidget(self.start_btn)
        vbox_start.setAlignment(Qt.AlignCenter)
        vbox_start.addSpacing(80)

        '''Data information layout'''
        vbox_data = QHBoxLayout()
        self.message = QLabel()
        self.message.setText('Information tips | Official account:[Python concentration camp]')
        self.message.setStyleSheet(
            'font-size:12px;')

        self.import_btn = QPushButton()
        self.import_btn.setText('Import data')
        self.import_btn.setFixedSize(90, 25)
        self.import_btn.clicked.connect(self.import_btn_click)

        vbox_data.addWidget(self.message)
        vbox_data.addStretch(1)
        vbox_data.addWidget(self.import_btn)

        '''Overall layout'''
        vbox = QVBoxLayout()
        vbox.addLayout(vbox_name)
        vbox.addLayout(vbox_start)
        vbox.addLayout(vbox_data)

        self.setLayout(vbox)

    def start_btn_click(self):
        if self.start_btn.text().strip() == 'Start roll call':
            self.thread_.start()
        else:
            self.start_btn.setText('Start roll call')

    def set_name(self, name):
        self.current_name.setText(name)

    def finished(self, finished):
        if finished is True:
            self.start_btn.setText('It's you')

    def import_btn_click(self):
        file = QFileDialog.getOpenFileName(self, 'Select file', os.getcwd(), 'Text File(*.txt)')
        file_path = file[0]
        print(file_path)
        fl = open(str(file_path), 'r', encoding='utf-8')
        self.data_list = fl.read().strip().split('\n')
        print(self.data_list)
        self.message.setText('Information tips | Successfully imported[' + str(len(self.data_list)) + ']Article personnel information')

Finally, add the main page to the main loop and run it directly.

Complete source code acquisition mode, official account reply to the "classroom roll call system", download directly.

I am a [Python camp. I am glad you saw it. Finally, I am a official account focused on Python knowledge sharing. I hope you can get your attention.

[previous highlights]

Start! Batch add Chinese watermark to PDF file

On the second day of the lunar new year, I made a windows notification manager!

Baidu picture downloader 2.0

gif dynamic picture generator, multiple pictures are combined to generate dynamic pictures

python several common data processing operations, one line of code can be completed!

Topics: Python