py tkinter 随机点名器


random-call-a-roll-machine

安装依赖:python 与其中的 tkinter 库

tkinter 获得方法:

pip install tkinter

可选设置:

# 这些设置位于开头
w1.title('settings') #select title
w1.geometry('500x500') #select size

# 这些设置位于中间绿框部分
size_of_students = 12 #字体大小
font_of_students = '楷体'
color_unchosen = 'light grey'
color_chosen = 'light blue'
students = {
     1:S('邓子玉')  #以逗号分割多对索引,每对索引形式为:(学号):S('(姓名)')
}
num_of_groups = 1 # 与下面保持一致
groups = {
    1: G('兼爱组', [39, 9 , 29, 40, 25, 8 ], 1) # 以逗号分割多个组索引,每对索引形式为:(组号):G('(组名)', [(学号列表,以逗号分割), 组号])
}
words_on_the_start_button = 'Go' # 控制点名按钮上显示文字

如果再遇到问题,请联系 20201108.

from sqlite3 import Row
import tkinter as tk
import random as rd
import tkinter.ttk as ttk

w1 = tk.Tk()
w1.title('settings') #select title
w1.attributes('-topmost',1)

frame1 = tk.Frame(w1)
frame1.pack()
frame_up = tk.Frame(frame1)
frame_down = tk.Frame(frame1)

w1.geometry('500x500') #select size

f1 = 1 # 个人/小组,不需设置
f2 = 1 # 可重/不可重,无需设置

def remove(x):
    print('students[x].group', students[x].group)
    if groups[students[x].group].unchosen.count(students[x].num) > 0:
        groups[students[x].group].unchosen.remove(students[x].num)
    if queue.count(students[x].num) > 0:
        queue.remove(students[x].num)

class S:
    def next(self):
        self.display.config(borderwidth = 0, relief = "raised", bg = color_chosen)
    def choose(self):
        self.status = 1
        self.display.config(bg = color_chosen)
        remove(self.num)
        self.display.config(borderwidth = 2, relief = "raised", bg = color_chosen)
    def click(self):
        if self.status == 0:
            self.status = 1
            self.display.config(bg = color_chosen)
            remove(self.num)
        else:
            self.status = 0
            self.display.config(bg = color_unchosen)
            if groups[self.group].unchosen.count(self.num) == 0:
                groups[self.group].unchosen.append(self.num)
            if queue.count(self.num) == 0:
                queue.append(self.num)
    def __init__(self, name, frame = frame_down):
        self.name = name
        self.status = 0
        self.group = 0
        self.num = 0
        self.display = tk.Button(frame, text = self.name, font = (font_of_students, size_of_students), bg= color_unchosen, borderwidth=0, command = self.click)

class G:
    def __init__(self, name, student, num):
        self.name = name
        self.stu = student
        self.unchosen = student
        self.num = 0
        for stu in student:
            students[stu].group = num
    def print(self, frame, row, column):
        self.name_display = tk.Label(frame, text = self.name + ': ', font = (font_of_students, size_of_students))
        self.name_display.grid(row = row, column = column)
        i = 0
        for stu in self.stu:
            students[stu].display.grid(row = row, column = column+2*i+1)
            tk.Label(frame, text = ' ').grid(row=row, column=2*i+2)
            i = i + 1
    def choose(self):
        k = self.unchosen[rd.randint(0, len(self.unchosen)-1)]
        print('lucky dog', k)
        students[k].choose()
        return k

#####  settings ################################################################################################################

size_of_students = 12 #字体大小
font_of_students = '楷体'
color_unchosen = 'light grey'
color_chosen = 'light blue'

students = {
     1:S('邓子玉'),  2:S('段霁桓'),  3:S('何秭杉'),  4:S('林宣朴'),  5:S('茹乐尧'),  6:S('孙煜翔'),  7:S('王方扬'),  8:S('户梓璇'),
     9:S('陈宁焕'), 10:S('陈奕如'), 11:S('邓梓乔'), 12:S('杜思成'), 13:S('杜宗泽'), 14:S('冯培臬'), 15:S('龚家诚'), 16:S('何煜城'),
    17:S('胡宸绮'), 18:S('黄正儒'), 19:S('蒋柏林'), 20:S('梁亦炜'), 22:S('刘知乐'), 23:S('卢简萌'), 24:S('骆  柯'), 25:S('汤宇禾'),
    26:S('唐家瑞'), 27:S('王沐鑫'), 29:S('吴思成'), 31:S('徐  铭'), 32:S('薛佩钧'), 33:S('杨驿宁'), 34:S('姚舜禹'),
    35:S('张宸瑜'), 36:S('张蕙元'), 37:S('张坤一'), 38:S('张鑫睿'), 39:S('张沂琛'), 40:S('周宇轩'), 41:S('邹厉为'), 42:S('唐乐华'),
    43:S('林子钧'), 44:S('吴庭瑞'), 45:S('陈名悦')
}
# 预设,无需管
for i in students:
    students[i].num = i

queue = list(students.keys())
num_of_groups = 4 #与下面的 groups 的数目保持一致
# 此处编号务必连续
groups = {
    1: G('兼爱组', [39, 9 , 29, 40, 25, 8 ], 1),
    2: G('问号组', [2 , 19, 12, 17, 23, 11], 2),
    3: G('问号组', [43, 20,  7,  5, 13, 31], 3),
    4: G('致知组', [33, 1 , 41, 24, 38, 4 ], 4),
    5: G('非攻组', [45, 22, 27, 42, 36, 34], 5),
    6: G('问号组', [15, 35, 32, 44, 26, 37], 6),
    7: G('问号组', [14, 6 , 18, 3 , 16, 10], 7)
}
queue_groups = list(groups.keys())
begin_line = 3

L = 10 # 确定人前的次数下限
R = 25 # 确定人前的次数上限
T = 0.1 # 确定人前每次停留时间
########  end settings   ######################################################################################################################

# roll

def roll():
    if f1 == 0 and f2 == 0:
        roll1()
    elif f1 == 0 and f2 == 1:
        roll2()
    elif f1 == 1 and f2 == 0:
        roll3()
    else:
        roll4()
    for i in groups:
        print(groups[i].unchosen)
    print(queue_groups)

# 单人,可重
pre = 0
def roll1():
    global pre
    temp = rd.sample(students.keys(), 1)
    if pre !=  0:
        students[pre].next()
    pre = temp[0]
    students[pre].choose()
    print('lucky dog:', pre)

# 单人,不可重
def roll2():
    global pre
    if pre !=  0:
        students[pre].next()
    pre = queue[rd.randint(0, len(queue)-1)]
    print('pre=', pre)
    students[pre].choose()
    print('lucky dog:', pre)

# 小组,可重
def roll3():
    print('roll3')
    global pre
    temp = rd.sample(groups.keys(), 1)
    if pre !=  0:
        students[pre].next()
    temp = temp[0]
    print('group:', temp)
    pre = groups[temp].choose()
    print('lucky dog:', temp)

# 小组,不可重
def roll4():
    print('roll4')
    global pre, queue_groups
    if pre !=  0:
        students[pre].next()
    temp = queue_groups[rd.randint(0, len(queue_groups)-1)]
    print('temp=', temp)
    pre = groups[temp].choose()
    print('lucky dog:', pre)
    queue_groups.remove(temp)
    if len(queue_groups) == 0:
        queue_groups = queue_groups = list(groups.keys())

# frame_up

textbox1 = tk.Label(frame_up, text='保证组组机会相等,组内人机会相等,', font=(font_of_students, 15))
textbox1.grid(row=2, column=0)
textbox2 = tk.Label(frame_up, text='抽到的人不会再被抽到', font=(font_of_students, 15))
textbox2.grid(row=3, column=0)

## cboxes
### cbox1
def choose1(event):
    global f1
    if(cbox1.get() == '单人模式'):
        textbox1.config(text= '保证人人机会相等,')
        f1 = 0
    else:
        textbox1.config(text= '保证组组机会相等,组内人机会相等,')
        f1 = 1

cbox1 = ttk.Combobox(frame_up)
cbox1.grid(row= 0, column= 0)
cbox1['value'] = ('单人模式','小组模式')
cbox1.current(1)
cbox1.bind("<>", choose1)

### cbox2
def choose2(event):
    global f2
    if(cbox2.get() == '可重模式'):
        textbox2.config(text= '抽到的人可能再被抽到')
        f2 = 0
    else:
        textbox2.config(text= '抽到的人不会再被抽到')
        f2 = 1
cbox2 = ttk.Combobox(frame_up)
cbox2.grid(row= 1, column= 0)
cbox2['value'] = ('可重模式', '不可重模式')
cbox2.current(1)
cbox2.bind("<>", choose2)
frame_up.pack(side= tk.TOP)

## add button
bt = tk.Button(frame_up, text = 'Go', command = roll)
bt.grid(row=5, column=0)


# frame_down

## display
for i in range(begin_line):
    tk.Label(frame_down, text= '').grid(row=i, column=0)

# i = 0
# t = []
# for stu in range(7):
#     t.append(tk.Label(frame_down, text='张坤一', bg = color_chosen, font = ('楷体', size_of_students)))
#     t[-1].grid(row = begin_line+1, column = 2*i+1)
#     tk.Label(frame_down, text = ' ').grid(row=begin_line+1, column=2*i+2)
#     i = i + 1
i = 1
for i in groups:
    groups[i].print(frame_down, begin_line+2*i, 0)
    tk.Label(frame_down, text=' ', font = (font_of_students, 2)).grid(row=begin_line+2*i+1)
    i = i+1
tk.Label(frame_down, text = 'generated by 20201108', fg ="grey", font = (font_of_students, 8)).grid(row= 100, column = 0, columnspan= 100)
tk.Label(frame_down, text = '20201102(@地狱石QAQ) AK IOI', fg = 'grey', font = (font_of_students, 8)).grid(row= 101, column= 0, columnspan= 100)
frame_down.pack(side= tk.BOTTOM)
# for i in t:
#     i.config(borderwidth = 2, relief = "raised")
w1.mainloop()

相关