Python3 tkinter 課堂隨機點名打分

2021-10-03 16:26:15 字數 4313 閱讀 5024

設計乙個學生課堂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")

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)

#建立listbox並為其新增內容

var1 = tk.strin**ar()

var1.set(file.list)

tk.listbox(window, listvariable=var1,font=("楷體",20),width = 30,height = 10).pack()

#設定可變標籤

str = "請點名"

var2 = tk.strin**ar()

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] = "".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] = "".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] = "".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()

python3 tkinter之Menu食用方法

usr bin python coding utf 8 from tkinter import def newfile print new file def openfile print open def about print this is a example of a menu root tk...

Python3 tkinter 對話方塊簡單布局

1 from tkinter import 2import tkinter.messagebox 3import datetime 4import time 5 tk tk 6 tk.geometry 800x600 窗體大小 7 tk.resizable false,false 固定窗體89 介面...

python3中,Tkinter編寫選單介面案例

python3中,tkinter編寫選單介面案例 from tkinter import top tk top.wm title 選單 top.geometry 400x300 300 100 建立乙個選單項,類似於導航欄 menubar menu top 建立選單項 fmenu1 menu top...