python随机点名(含UI界面)

经过前面数个版本的改近,最终还是加上了UI界面

import sys
import os
if sys.version_info[0] == 2:
    import Tkinter
    from Tkinter import *
else:
    import tkinter as Tkinter
    from tkinter import *
import random

data = []
going = True
is_run = False


try:
    classes_path = os.path.expanduser('1.txt')
    with open(classes_path,'r',encoding = 'UTF-8') as f:
        data = f.readlines()
    data = [c.strip() for c in data]

except:
    classes_path = os.path.expanduser('1.txt')
    with open(classes_path,'r',encoding = 'gbk') as f:
        data = f.readlines()
    data = [c.strip() for c in data]
 
 
def lottery_roll(var1, var2):
    global going
    show_member = random.choice(data)
    var1.set(show_member)
    if going:
        window.after(50, lottery_roll, var1, var2)
    else:
        var2.set('欸嘿'.format(show_member))
        going = True
        return
 
 
def lottery_start(var1, var2):
    global is_run
    if is_run:
        return
    is_run = True
    var2.set('看看抽到了谁')
    lottery_roll(var1, var2)
 
 
def lottery_end():
    global going, is_run
    if is_run:
        going = False
        is_run = False
 
 
if __name__ == '__main__':
    window = Tkinter.Tk()
    window.geometry('405x320+250+15')
    window.title('      随机人名')
 
    bg_label = Label(window, width=70, height=24)
    bg_label.place(anchor=NW, x=0, y=0)
 
    var1 = StringVar(value='即 将 开 始')
    show_label1 = Label(window, textvariable=var1, justify='left', anchor=CENTER, width=17, height=3,font='楷体 -40 bold', foreground='black')
    show_label1.place(anchor=NW, x=21, y=20)
    var2 = StringVar(value='是你吗  ')
    show_label2 = Label(window, textvariable=var2, justify='left', anchor=CENTER, width=38, height=3,font='楷体 -18 bold')
    show_label2.place(anchor=NW, x=21, y=240)
 
    button1 = Button(window, text='开始', command=lambda: lottery_start(var1, var2), width=14, height=2,font='宋体 -18 bold')
    button1.place(anchor=NW, x=20, y=175)
    button2 = Button(window, text='结束', command=lambda: lottery_end(), width=14, height=2, font='宋体 -18 bold')
    button2.place(anchor=NW, x=232, y=175)
 
    window.mainloop()

当然大家也可以直接下载作者打包好的版本(不需要python环境,Windows可执行)

链接:https://pan.baidu.com/s/1-7_b7Nmx2nkoKV90Ugnvzw
提取码:0000

如有更好的解决办法可在评论区发表,或者邮箱发给作者

邮箱:[email protected]

你可能感兴趣的:(python,ui,开发语言)