应人之邀,写一个学生和作业进行随机匹配并通过GUI窗口显示的程序。
from tkinter import *
import random
import tkinter.font as tkFont
import csv
def start():
fun()
# 学生名字长度要与作业长度匹配
names = ['','',''
]
homework = ['b311',
'D068',
'D069',
'D070',
'D071',
'D072',
'D073',
'D074',
'D075',
'D076',
'D077',
'D078',
'D079',
'D080',
'D081',
'D082',
'D083',
'D084',
'D085',
'D086',
'D087',
'D088',
'D089',
'D090',
'D091',
'D092',
'D093',
'D094',
'D095',
'D096',
'D097',
'D098',
'D099',
'D100',
'D101',
'D102',
'D103',
'D104',
'D105',
'D106',
'D107',
'D108',
]
result = []
def fun():
student = names[random.randint(0, len(names)-1)]
work = homework[random.randint(0, len(homework)-1)]
names.remove(student)
homework.remove(work)
result.append([student, work])
with open('作业匹配.csv','w+', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(result)
root.update() #不断更新
root.after(1)
Label(root, text= student+' '+work , font = ("华文宋体",60),fg = "red" , width = 18,height = 2).place(x = 300,y = 500,anchor = 'nw')
root = Tk()
myfont = tkFont.Font(family='华文宋体', size=50, weight=tkFont.BOLD, slant=tkFont.ITALIC, underline=0, overstrike=0)
myfont1 = tkFont.Font(family='华文宋体', size=50, weight=tkFont.BOLD, slant=tkFont.ITALIC, underline=0, overstrike=0)
root.geometry('1400x900',) # 窗口大小
lb1 = Label(root, text='作业匹配', font=myfont,)
lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)
btn1 = Button(root, text='开始作业匹配', command=start,font=myfont, bg = "orange")
btn1.place(relx=0.3, rely=0.4, relwidth=0.4, relheight=0.1)
# btn2 = Button(root, text='结束', command=stop,font=myfont, bg = "gray")
# btn2.place(relx=0.55, rely=0.4, relwidth=0.3, relheight=0.1)
root.mainloop()
效果图如下