抖音同款课堂点名系统PyQt5写起来很简单

刷抖音的时候发现一个老师在用的课堂点名系统。用PyQt5实现了一下同款,导入学生姓名,测试了一下完美运行。

【阅读全文】

操作效果展示:

抖音同款课堂点名系统PyQt5写起来很简单

完整的源代码块仍然放在本文的后面,所以您需要直接运行到文章的末尾才能获得下载模式。

[En]

The complete source code block is still placed at the back of the article, so you need to run it directly to the end of the article to get the download mode.

在使用时准备好学生姓名文件,并使用按钮直接导入数据开始点名。创建新的文本文档并设置名称设置。名称文件的示例如下所示。

[En]

Prepare the file of the student’s name when you use it, and use the button to import the data directly to start the roll call. Create a new text document and set the name settings. An example of a name file is shown below.

抖音同款课堂点名系统PyQt5写起来很简单

使用系统库或第三方库比较常见,这里就不一一介绍了。

[En]

It is common to use system libraries or third-party libraries, so we will not 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

为了和UI界面的主线程分离开来,我们采用的还是QThread多线程的方式来实现随机点名模块的。这样做的目的是为了防止主线程阻塞,下面是子线程的实现部分。

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('遍历次数:', 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('无数据')

UI 界面的实现部分也比较常规,下面主要实现部分的代码块。

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

    def init_ui(self):
        '''子线程调用'''
        self.thread_ = WorkThread(self)
        self.thread_.trigger.connect(self.set_name)
        self.thread_.finished.connect(self.finished)

        '''应用初始化信息'''
        self.setWindowTitle('课堂点名系统  公众号:[Python 集中营]')
        self.setWindowIcon(QIcon('课堂点名.ico'))
        self.setFixedSize(500, 350)

        '''姓名信息布局'''
        vbox_name = QVBoxLayout()
        self.current_name = QLabel()
        self.current_name.setText('随机点名啦')
        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)

        '''开始信息布局'''
        vbox_start = QVBoxLayout()
        self.start_btn = QPushButton()
        self.start_btn.setText('开始点名')
        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)

        '''数据信息布局'''
        vbox_data = QHBoxLayout()
        self.message = QLabel()
        self.message.setText('信息提示 | 公众号:[Python 集中营]')
        self.message.setStyleSheet(
            'font-size:12px;')

        self.import_btn = QPushButton()
        self.import_btn.setText('导入数据')
        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)

        '''整体布局'''
        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() == '开始点名':
            self.thread_.start()
        else:
            self.start_btn.setText('开始点名')

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

    def finished(self, finished):
        if finished is True:
            self.start_btn.setText('就是你了')

    def import_btn_click(self):
        file = QFileDialog.getOpenFileName(self, '选择文件', 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('信息提示 | 成功导入[' + str(len(self.data_list)) + ']条人员信息')

最后,将主页添加到主循环中并直接运行它。

[En]

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

完成源码访问,回复公众号中的《课堂点名系统》,直接下载。

[En]

Complete source code access, reply to the “classroom Roll call system” in the official account and download it directly.

抖音同款课堂点名系统PyQt5写起来很简单

我是 [Python 集中营]、很高兴您看到了最后, 我是一个专注于 Python 知识分享的公众号,希望可以得到您的关注~

【往期精彩】

开工啦!批量向PDF文件添加中文水印…

大年初二、做了一个windows通知管理器!

百度图片下载器2.0

gif动态图片生成器,多张图片组合后生成动图…

python几个常见的数据处理操作,一行代码就能完成!

Original: https://www.cnblogs.com/lwsbc/p/15872907.html
Author: Python集中营
Title: 抖音同款课堂点名系统PyQt5写起来很简单

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/510110/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球