2023年7月24日 星期一
2023年7月23日 星期日
小小測驗程式碼
#修改 秀圖片代表對錯
# 匯入函式庫
import csv
import tkinter as tk
import os,sys #停止程式運作模組
#開啟csv檔案
with open("t11.csv", "r", encoding="utf-8") as fp:#t11.csv是測驗題
data = list(csv.reader(fp))#題目
with open("t11.csv", "r", encoding="utf-8") as fp2:
ansr = list(csv.reader(fp2))#答案
row = "測驗題"
answer = ""
input_answer = ""
score = 0
q=0
def input_if():#判斷答案
global answer
global input_answer
global score
if answer == input_answer:
score+=1
input_answer = ("你輸入的答案是:"+input_answer+",你答對了!\n")
else:
input_answer = ("你輸入的答案是:"+input_answer+"你答錯了!\n"+row+"正確答案是"+answer)
switchButtonState()#切換ABC輸入鍵
bt_plus['state'] = tk.NORMAL#出題鍵正常
result2.set(input_answer)#傳送作答結果
def split_A_D():#製作題目函式
global q
global row
global input_answer
global answer
ansr0 = ansr[q]
del ansr0[0]
data0 = data[q]
del data0[1]
for answer in ansr0:
print(answer)
for row in data0:
q+=1 #題號加1
input_answer =""#將回饋清除
result2.set(input_answer)
result.set(row)
switchButtonState()#切換ABC鍵
bt_start['state'] = tk.DISABLED#關閉出題鍵
bt_plus['state'] = tk.DISABLED#關閉出題鍵
def score_total():#秀出成績函式
global data
input_answer = ("測驗結束了!\n"+"總共"+str(len(data))+"題,"+"你答對"+str(score)+"題。")
result2.set(input_answer)
bt_end['state'] = tk.NORMAL#結束鍵恢復功能
bt_plus['state'] = tk.DISABLED#關閉出題鍵
def switchButtonState():#切換按鍵函式
if (btn1['state'] == tk.NORMAL):
btn1['state'] = tk.DISABLED #不能按狀態
if (btn2['state'] == tk.NORMAL):
btn2['state'] = tk.DISABLED #不能按狀態
if (btn3['state'] == tk.NORMAL):
btn3['state'] = tk.DISABLED #不能按狀態
else:
btn1['state'] = tk.NORMAL#開啟按鍵
btn2['state'] = tk.NORMAL
btn3['state'] = tk.NORMAL
def q_value():#判斷題號函式
if q >= 10:
score_total()
else:
split_A_D()#繼續出題
bt_plus = tk.Button(root, text="下一題", command=q_value,font=('Arial',30))
def test_end():#結束函式
os._exit(0)
def inp_ansr1():
global input_answer # inpup_answer 是全域變數
input_answer = "a"
input_if()#呼叫判斷函式
def inp_ansr2():
global input_answer # inpup_answer 是全域變數
input_answer = "b"
input_if()
def inp_ansr3():
global input_answer # inpup_answer 是全域變數
input_answer = "c"
input_if()
# 宣告
root = tk.Tk()
# 視窗標題
root.title('小小測驗')
# 視窗初始大小
root.geometry('800x600')
# 視窗[左右, 上下]是否可拉大拉小,若都為0則視窗右上角的最大化按鈕會無法點擊
root.resizable(0, 1)
# 定義變數k
row = "歡迎進入小小測驗"
# 定義tkinter專屬變數result
result = tk.IntVar()
result2 = tk.StringVar()
# ****將變數k單次傳值至tkinter專屬變數result*****
result.set(row)
result2.set(input_answer)
# 建立標籤
lab_result = tk.Label(root, text='textvariable有優先權,所以這句不會顯示', textvariable=result,font=('Arial',30))#顯示測驗題目
mylabel = tk.Label(root, textvariable=result2, font=('Arial',20)) # 顯示輸入及判斷結果
# Button 設定 command 參數
btn1 = tk.Button(root, text='答案a',state=tk.DISABLED, font=('Arial',20,'bold'), command=inp_ansr1 )#作答
btn2 = tk.Button(root, text='答案b',state=tk.DISABLED, font=('Arial',20,'bold'), command=inp_ansr2 )
btn3 = tk.Button(root, text='答案c',state=tk.DISABLED, font=('Arial',20,'bold'), command=inp_ansr3 )
#建立輸入框
#bt_box = tk.Entry(width = 4,font=("Helvetica",14))
# 建立按鈕,並且綁定事件
#bt_input = tk.Button(root, text='確定',state=tk.DISABLED, command=input_if)#確定鍵失效
bt_start = tk.Button(root, text="開始", command=q_value,font=('Arial',20))
bt_plus = tk.Button(root, text="下一題",state=tk.DISABLED, command=q_value,font=('Arial',20))
bt_end = tk.Button(root, text='離開', state=tk.DISABLED,command=test_end,font=('Arial',20))#結束漸失效
# 為物件分配位置
lab_result.pack()# 自動佈置
mylabel.pack()
btn1.place(x =200, y = 150)
btn2.place(x =350, y = 150)
btn3.place(x =500, y = 150)
bt_plus.place(x =10, y = 0)
bt_start.place(x =200, y =300)
bt_end.place(x =500, y =300)
# 自動刷新畫面函式
root.mainloop()