设计一个学生课堂tkinter界面,包括随机点名,回答问题加分减分,保存修改内容并退出等功能。
示例操作如下:
包含三个文件:
姓名#回答次数#课堂分数
后裔#0#50
妲己#0#50
兰陵王#0#50
虞姬#0#50
白起#0#50
鲁班#0#50
瑶#0#50
王昭君#0#50
凯爹#0#50
韩信#0#50
孙尚香#0#50
娜可露露#0#50
list = []
with open("File.txt","r+",encoding = "utf-8")as f:
count = len(f.readlines())
f.seek(0)
for i in range(count):
every = f.readline()
every = every.strip("\n")
list.append(every.split("#",4))
for i in range(len(list)):
for j in range(len(list[i])):
length = len(list[i][j])
if length == 4:
pass
else:
list[i][j] += (4-length)*"__"
import tkinter as tk
import File
import random
import threading
from time import sleep
#新建主窗口
window = tk.Tk()
window.title("憨憨学校点名系统")
window.minsize(800,600)
window.maxsize(800,600)
str_= "防伪QQ:3262787587"
#创建Listbox并为其添加内容
var1 = tk.StringVar()
var1.set(File.list)
tk.Listbox(window, listvariable=var1,font=("楷体",20),width = 30,height = 10).pack()
#设置可变标签
str = "请点名"
var2 = tk.StringVar()
var2.set(str)
l1 = tk.Label(window, textvariable=var2, background="red", font=("楷体", 30), width=20, height=1)
l1.pack()
list3 = [0]
#创建标签变换函数
def round1():
counter = 7
while counter:
num=random.randint(1,len(File.list)-1)
var2.set(File.list[num][0].strip("_"))
sleep(0.20)
counter -= 1
list3[0] = num
File.list[num][1] = "{0}".format(int(File.list[num][1].strip("_"))+1)
length = len(File.list[num][1])
if length == 4:
pass
else:
File.list[num][1] += (4-length)*"__"
var1.set(File.list)
#创建回答正确函数
def round2():
index = list3[0]
File.list[index][2] = "{0}".format(int(File.list[index][2].strip("_"))+1)
length = len(File.list[index][2])
if length == 4:
pass
else:
File.list[index][2] += (4-length)*"__"
var1.set(File.list)
#创建回答错误函数
def round3():
index = list3[0]
File.list[index][2] = "{0}".format(int(File.list[index][2].strip("_"))-1)
length = len(File.list[index][2])
if length == 4:
pass
else:
File.list[index][2] += (4-length)*"__"
var1.set(File.list)
#创建保存并退出函数
def round4():
with open("File.txt","w",encoding = "utf-8")as f:
for i in range(len(File.list)):
for j in range(len(File.list[i])):
File.list[i][j] = File.list[i][j].strip("_")
if j < 2:
File.list[i][j] += "#"
else:
File.list[i][j] += "\n"
f.writelines(File.list[i])
window.destroy()
# 创建线程的函数
def newtask1():
t = threading.Thread(target=round1)
t.start()
# 创建线程的函数
def newtask2():
t = threading.Thread(target=round2)
t.start()
# 创建线程的函数
def newtask3():
t = threading.Thread(target=round3)
t.start()
#设置随机点名按钮
#在窗口界面设置放置Button按键
b = tk.Button(window, text="随机点名", font=("楷体", 20), width=10, height=1, command=newtask1)
b.place(x=50, y=400, anchor="nw")
#设置回答正确按钮
#在窗口界面设置放置Button按键
b = tk.Button(window, text="回答正确", font=("楷体", 20), width=10, height=1, command=newtask2)
b.place(x=320, y=400, anchor="nw")
#设置回答错误按钮
#在窗口界面设置放置Button按键
b = tk.Button(window, text="回答错误", font=("楷体", 20), width=10, height=1, command=newtask3)
b.place(x=600, y=400, anchor="nw")
#设置保存并退出按钮
#在窗口界面设置放置Button按键
b = tk.Button(window, text="保存并退出", font=("楷体", 20), width=10, height=1, command=round4)
b.place(x=320, y=500, anchor="nw")
#设置不可变标签
str1 = "请老师根据学生回答情况选择按钮,回答正确加1分,回答错误减1分,初始分数50分"
#在图形界面上设定标签
l2 = tk.Label(window, text=str1, font=("楷体", 15), width=77, height=1)
l3 = tk.Label(window, text=str_, font=("楷体", 15), width=77, height=1)
print(str_)
l2.pack(side="bottom")
l3.pack(side="bottom")
window.mainloop()