python 版随机点名器

import random
import sys
import os
from PyQt5 import QtGui
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *


class MyApp(QMainWindow, QWidget):
    def __init__(self):
        super().__init__()
        # 名字的区域
        self.name_list = []
        with open("test.txt", encoding='utf-8') as f:
            for line in f.readlines():
                line = line.strip('\n')  # 去掉列表中每一个元素的换行符
                self.name_list.append(line)
        # 图片的区域
        self.images = []
        for root, dirs, files in os.walk(r'E:\home\webContainer\ntmssResource'):
            # 遍历文件
            for f in files:
                self.images.append(os.path.join(root, f))
        self.App()

    def App(self):
        # 设置主体框架
        # 设置窗体名字
        self.setWindowTitle('随机点名器')
        # 设置窗体大小
        self.resize(1000, 600)

        # 设置字体大小
        self.font = QtGui.QFont()
        self.font.setPointSize(23)

        # 设置start按钮
        self.start_button = QPushButton('Start', self)
        self.start_button.resize(200, 100)
        self.start_button.move(650, 100)
        self.start_button.setFont(self.font)

        # 设置stop按钮
        self.stop_button = QPushButton('Stop', self)
        self.stop_button.resize(200, 100)
        self.stop_button.move(650, 450)
        self.stop_button.setFont(self.font)

        # 设置名字显示
        self.name_lable = QLineEdit(self)
        self.name_lable.move(100, 450)
        self.name_lable.resize(350, 100)
        self.name_lable.setText('这个人是谁呢?')
        self.name_lable.setAlignment(Qt.AlignCenter)
        self.name_lable.setFont(self.font)

        # 设置图片显示
        self.image_lable = QLabel(self)
        # 为图片显示设置一个画布 位置大小
        self.image_lable.setGeometry(50, 50, 470, 300)
        # 为图片设置属性
        self.image_lable.setStyleSheet("border: 1px solid blue")
        # 插入图片
        self.showPics()

        # 设置开始和结束
        self.start_button.clicked.connect(lambda: self.start_app())
        self.stop_button.clicked.connect(lambda: self.stop_app())
        self.stop_button.clicked.connect(lambda: self.btn_enabled_app(self.start_button))

    # 开始程序
    def start_app(self):
        self.start_button.setEnabled(False)  # 将start按钮设置成禁止点击
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.setname_image)
        self.timer.start(1000)  # 图片播放的时间

    # 程序结束
    def stop_app(self):
        self.timer.stop()

    #
    def btn_enabled_app(self, btn):
        # 按下按钮后解除禁止可以继续点击
        btn.setEnabled(True)

    def setname_image(self):
        # 点名系统
        name = self.name_list[random.randint(0, len(self.name_list) - 1)]
        self.name_lable.setText(name)
        self.name_lable.setAlignment(Qt.AlignCenter)  # 设置文本对齐方式 居中对齐
        self.name_lable.setFont(self.font)
        # 插入图片
        self.showPics()

    def showPics(self):
        imagename = self.images[random.randint(0, len(self.images) - 1)]
        self.pnx = QPixmap(imagename)  # 加载图片路径
        self.image_lable.setPixmap(self.pnx)  # 将图片显示画布上
        self.image_lable.setScaledContents(True)  # 图片自适应窗口


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()
    sys.exit(app.exec_())

 

posted @ 2021-11-19 16:14  Bonnie_ξ  阅读(556)  评论(0编辑  收藏  举报