首页 > Python资料 博客日记

【python GUI】使用python开发一个随机点名系统(包含代码解释)

2025-01-14 15:00:11Python资料围观11

这篇文章介绍了【python GUI】使用python开发一个随机点名系统(包含代码解释),分享给大家做个参考,收藏Python资料网收获更多编程知识


一、GUI中常用的库

1、Tkinter

Tkinter是Python标准库中的一个GUI(Graphical User Interface,图形用户界面)工具包,其目的是为Python开发者提供快捷创建GUI应用程序的方式。
Tkinter基于Tcl/Tk图形库,允许我们使用Python代码来创建和管理窗口、标签、按钮、复选框、文本框、列表框、滚动条、画布、菜单等多种控件和组件

1.1 创建窗口

import tkinter as tk

root = tk.Tk()  # 创建窗口
root.title('演示窗口')  # 窗口标题
root.geometry("300x100+630+80")  # (宽度x高度)+(x轴+y轴)
root.mainloop()  # 让窗口一直显示,循环

1.2 创建按钮并绑定事件

创建点击按钮事件的弹窗,先导入messagebox,这个必须单独导入

from tkinter import messagebox

btn1 = tk.Button(root)  # 创建按钮,并且将按钮放到窗口里面
btn1["text"] = "点击"  # 给按钮一个名称
btn1.pack()  # 按钮布局
def test(e):
    '''创建弹窗'''
    messagebox.showinfo("窗口名称", "点击成功")
btn1.bind("<Button-1>", test)  # 将按钮和方法进行绑定,也就是创建了一个事件
root.mainloop()  # 让窗口一直显示,循环

点击按钮后想要触发事件,并弹出弹框提示,需要导入messagebox,这个必须单独导入

2、Tkinter ttk

我们很多人都开始追求潮流了,但tkinter显然有点太“粗暴”了,不太美观
你以为作者开发ttk是吃饱了撑的吗?不是的。这是为了美观,ttk的部件比tkinter的好一些

按钮:

tkinter.ttk.Button(master=None, **kw)

单选框:

tkinter.ttk.Checkbutton(master=None, **kw)

选择框:

tkinter.ttk.Combobox(master=None, **kw)

单行文本框:

tkinter.ttk.Entry(master=None, **kw)

进度条:

tkinter.ttk.Progressbar(master=None,**kw)

其中:master:组件的父容器 ,**kw:组件的各种参数

3、Lable

语法如下:

Label(父对象, options, ...)

options参数,如下:

参数作用
font指定 Label 中文本的字体(注:如果同时设置字体和大小,应该用元组包起来,如(“楷体”, 20)
foreground(fg)设置 Label 的文本和位图的颜色
textvariableLabel 显示 Tkinter 变量(通常是一个 StringVar 变量)的内容

4、PIL

PIL(Python Image Library)库是Python语言的第三方库,需要通过pip工具安装。安装PIL库的方法如下,需要注意,安装库的名字是pillow
PIL库支持图像储存、显示和处理,他能够处理几乎所有图片格式,可以完成对图像的缩放、剪裁、叠加以及向图像添加线条、图像和文字等操作。

5、filedialog

filedialog是tkinter中的文件对话框

tkinter.filedialog.asksaveasfilename():选择以什么文件名保存,返回文件名
tkinter.filedialog.asksaveasfile():选择以什么文件保存,创建文件并返回文件流对象
tkinter.filedialog.askopenfilename():选择打开什么文件,返回文件名
tkinter.filedialog.askopenfile():选择打开什么文件,返回IO流对象
tkinter.filedialog.askdirectory():选择目录,返回目录名
tkinter.filedialog.askopenfilenames():选择打开多个文件,以元组形式返回多个文件名
tkinter.filedialog.askopenfiles():选择打开多个文件,以列表形式返回多个IO流对象

二、点名系统的代码及解释



from tkinter import *    # Tkinter是Python标准库中的一个GUI(Graphical User Interface,图形用户界面)工具包
import random
import re  # 正则
import time
import threading  # Python Threading 模块提供了一个高级的接口来创建和管理线程
from tkinter import messagebox  # 创建点击按钮事件的弹窗,先导入messagebox,这个必须单独导入
from tkinter import ttk  # 美化 Tkinter 的
from base64 import b64decode  # 加密算法
from PIL import Image,ImageTk   # PIL库用于图像储存、显示和处理
from tkinter.filedialog import askopenfilename  # filedialog是tkinter中的文件对话框


class APP:
    def __init__(self):
        self.root = Tk()  # 创建一个窗口
        self.time_span=0.03   # 名字显示间隔
        self.root.title('东湖点名册')   # 窗口标题
        width = 750
        height = 430
        left = (self.root.winfo_screenwidth() - width) / 2   # winfo_screenwidth()是获取你的电脑屏幕的大小
        top = (self.root.winfo_screenheight() - height) / 2
        self.root.geometry("%dx%d+%d+%d" % (width, height, left, top))   # 窗口所在的位置
        self.create_widget()   # 创建控件方法
        self.set_widget()    # 设置控件方法
        self.place_widget()   # 放置控件方法
        self.root.mainloop()  # 循环,让窗口一直展示

    def create_widget(self):
        self.label_show_name_var=StringVar()
        self.label_show_name=ttk.Label(self.root,textvariable=self.label_show_name_var,font=('Arial', 62,"bold"),foreground = '#1E90FF')      # 上面有解释
        self.btn_start=ttk.Button(self.root,text="come on",)    # 创建按钮
        self.btn_load_names=ttk.Button(self.root,text="上传名单文件",)   # 创建按钮
        self.lf1=ttk.LabelFrame(self.root,text="选吧,都一样")
        self.radioBtn_var=IntVar()
        self.radioBtn_var.set(1)
        self.radioBtn_random1=ttk.Radiobutton(self.lf1,text="随机点名",variable=self.radioBtn_var, value=1)
        self.radioBtn_random2=ttk.Radiobutton(self.lf1,text="随kun点名",variable=self.radioBtn_var, value=2)
        self.radioBtn_random3=ttk.Radiobutton(self.lf1,text="随鸡点名",variable=self.radioBtn_var, value=3)
        self.radioBtn_random4=ttk.Radiobutton(self.lf1,text="random点名",variable=self.radioBtn_var, value=4)
        self.label_show_name_num=ttk.Label(self.root,font=('Arial', 32),foreground = '#FF7F50')
        paned = PanedWindow(self.root)
        img_= '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCADIAPADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6er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y+yjFaVv4Z0y3wTCZCO7nNYSzOjHbU6IZXWe+h51CksrYjjZ8+gzXrnwT0ie1m1G8uE2blWNQevcn+lRRwwW64hiRB6AYru/A8W3R/Ox/rnLfh0H8q4MRj3Wi4JWR6OHy9UZKbd2dFRRRXAegFFFFABUdz/wAe8v8AuH+VSVHc/wDHvL/uH+VAB58X/PWP/voUefF/z1j/AO+hRRQAefF/z1j/AO+hR58R/wCWsf8A30KKKAPDobVrPxJ4js4428mO/eSNgvBWQB+PxYirRjc/wN+VFFQykJ5cn9x/yo2P/cb8qKKSQ2Hlyf3G/KneW3/PNv8AvmiihsBTAx6xH/vmgWiMPmgBz6rRRVXfcmyYo0+D/n1T/vigafB/z6x/98UUU+eXcXJHsSrY24I/0WP/AL4qwlrCvSBB/wAAooo5pdxcsexOqBekePwqRc+n6UUUhoU5HY0xi3YH8qKKQ76kTbh/CaYzN2VvyoooKMrXLqe206eWKN2kxtQKpPzHgfqa9Z8PLHaaNaQmRRsjAwSKKKaJkaPnxf8APVP++hSefH/z0T/voUUU7kiieL/non/fQo86L/non/fQoooAXz4v+eqf99Co7meL7PL+9T7h/iHpRRQM/9k='
        the_img = b64decode(img_)       #将图片硬编码到GUI
        paned.image = ImageTk.PhotoImage(data=the_img)
        self._img = Label(self.root, image=paned.image,background='white')

    def set_widget(self):
        default_name_="猜猜是哪个幸运儿?"
        self.label_show_name_var.set(default_name_)
        self.label_show_name_adjust(default_name_)
        self.btn_start.config(command=lambda :self.thread_it(self.start_point_name))
        self.btn_load_names.config(command=self.load_names)
        init_names=self.load_names_txt("./names.txt")
        self.root.protocol('WM_DELETE_WINDOW',self.quit_window)
        self.root.bind('<Escape>',self.quit_window)
        if init_names:
            self.default_names=init_names   #1.文件存在但是无内容。2.文件不存在
            self.label_show_name_num.config(text=f"一共加载了{len(self.default_names)}个姓名")
        else:
            self.btn_start.config(state=DISABLED)
            self.label_show_name_num.config(text=f"请先手动导入人名单!")

    def place_widget(self):
        self.lf1.place(x=340,y=160,width=350,height=100)
        self.radioBtn_random1.place(x=40,y=0,width=500,height=30)
        self.radioBtn_random2.place(x=200,y=0,width=100,height=30)
        self.radioBtn_random3.place(x=40,y=40,width=100,height=30)
        self.radioBtn_random4.place(x=200,y=40,width=100,height=30)
        self.btn_start.place(x=380,y=320,width=100,height=30)   # come on按钮的位置
        self.btn_load_names.place(x=530,y=320,width=100,height=30)    # 上传文件按钮的位置
        self._img.place(x=45, y=160, height=200, width=240)    # 图像大小和位置
        self.label_show_name_num.place(x=300,y=260)   # 请先手动导入人名单的位置

    def label_show_name_adjust(self,the_name):  # 判断名字字数,根据字数放到对应的位置
        if len (the_name)==1:
            self.label_show_name.place(x=290, y=15)
        elif len(the_name) == 2:
            self.label_show_name.place(x=270, y=15)
        elif len(the_name) == 3:
            self.label_show_name.place(x=220, y=15)
        elif len(the_name) == 4:
            self.label_show_name.place(x=180, y=15)
        elif len(the_name) == 5:
            self.label_show_name.place(x=140, y=15)
        else:
            self.label_show_name.place(x=2, y=15)

    def start_point_name(self):
        """
        启动之前进行判断,获取点名模式
        """
        if len(self.default_names)==1:
            messagebox.showinfo("提示",'人名单就一个人,不用选了!')
            self.label_show_name_var.set(self.default_names[0])
            self.label_show_name_adjust(self.default_names[0])
            return
        if self.btn_start["text"]=="come on":
            self.btn_load_names.config(state=DISABLED)
            self.running_flag=True
            if isinstance(self.default_names,list):
                # 按下停止按钮执行的方法
                self.btn_start.config(text="你是倒霉蛋")
                # 选择框中每个选项对应执行的方法模块
                if self.radioBtn_var.get()==1 or self.radioBtn_var.get()==2 or self.radioBtn_var.get()==3 or self.radioBtn_var.get()== 4:
                    mode="random"
                else:
                    pass
                self.thread_it(self.point_name_begin(mode))

            else:
                messagebox.showwarning("警告","请先导入人名单!")
        else:
            self.running_flag=False
            self.btn_load_names.config(state=NORMAL)
            self.btn_start.config(text="come on")

    def point_name_begin(self,mode):
        # 随机方法模块
        if mode=="random":
            while True:
                    if self.running_flag:
                        random_choice_name=random.choice(self.default_names)
                        self.label_show_name_var.set(random_choice_name)
                        self.label_show_name_adjust(random_choice_name)
                        time.sleep(self.time_span)
                    else:
                        break

    def always_ergodic(self):
        """
        遍历此列表
        """
        for i in self.default_names:
            if self.running_flag:
                self.label_show_name_var.set(i)
                self.label_show_name_adjust(i)
                time.sleep(self.time_span)
                if i==self.default_names[-1]:
                    self.always_ergodic()
            else:
                break
    def load_names(self):
        """
        手动加载txt格式人名单
        """
        filename = askopenfilename(
                filetypes = [('文本文件', '.TXT'), ],
                title = "请选择点名册",
            initialdir="./"
                )
        if filename:
            names=self.load_names_txt(filename)
            if names:
                self.default_names=names
                # 获取中文名字的数量
                no_Chinese_name_num=len([n for n in names if not self.load_name_check(n)])
                if no_Chinese_name_num==0:
                    pass
                else:
                    messagebox.showwarning("请注意",f'导入名单有{no_Chinese_name_num}个不是中文名字')
                self.label_show_name_num.config(text=f"一共加载了{len(self.default_names)}个姓名")
                default_name_ = "马上要开始了哦!!"
                self.label_show_name_var.set(default_name_)
                self.label_show_name_adjust(default_name_)
                self.btn_start.config(state=NORMAL)
            else:
                messagebox.showwarning("警告","导入失败,请检查!")

    def load_names_txt(self,txt_file):
        """
        读取txt格式的人名单
        """
        # io流文件读取
        try:
            with open(txt_file,'r',encoding="utf-8")as f:
                names=[name.strip() for name in f.readlines()]
                if len(names)==0:
                    return False
                else:
                    return names
        except:
            return False

    def load_name_check(self,name):
        """
        对txt文本中的人名进行校验
        中文汉字->True
        非中文汉字->False
        """

        # 使用正则对名字进行校验是否符合规范
        regex = r'[\u4e00-\u9fa5]+'
        if re.match(regex,name):
            return True
        else:
            return False

    def thread_it(self,func,*args):
        t=threading.Thread(target=func,args=args)
        t.setDaemon(True)
        t.start()

    def quit_window(self,*args):
        """
        程序退出触发此函数
        """
        ret=messagebox.askyesno('退出','确定要退出?')
        if ret:
            self.root.destroy()

if __name__ == '__main__':
    a=APP()

版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐