基于python怎么实现随机点名系统-kb88凯时官网登录

时间:2023-05-26
阅读:
免费资源网 - https://freexyz.cn/

效果展示

创建一个这样的文件夹,然后把要随机点名的名字写在里面

导入后,这里就显示你导入了多少人员信息

点击开始点名后,会随机从导入名字里挑选一位幸运儿~

效果大概就是这样,下面我们来看看代码吧

代码展示

导入模块

里面有第三方模块,需要大家自己安装一下

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
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(6, 10)
            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('无数据')
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('课堂点名系统')
        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('信息提示')
        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))   ']条人员信息')

运行

if __name__ == '__main__':
    app = qapplication(sys.argv)
    app.setstylesheet(load_stylesheet_pyqt5())
    main = classcollsystem()
    main.show()
    sys.exit(app.exec_())
免费资源网 - https://freexyz.cn/
返回顶部
顶部
网站地图