甲班python例外處理try:...except:...

import tkinter   #輸入tkinter繪圖模組 #財金程式設計602

class Financial:                    #類別用於建構504道金融常識題目
    def __init__(self, number, q, a, b, c, d, ans): 
        self.number, self.q, self.ans = number, q, ans
        self.a, self.b, self.c, self.d = a, b, c, d

def check(choice):
    choice = reply.get()            #以字串存'1'...'4'
    temp = number.get() - 1         #題號
    if choice == '1': 
        LBa.config(bg="#a4abc1")
        if choice == List[temp].ans: LBa.config(bg='blue',fg='white')  
    if choice == '2': 
        LBb.config(bg="#a4abc1")
        if choice == List[temp].ans: LBb.config(bg='blue',fg='white')  
    if choice == '3': 
        LBc.config(bg="#a4abc1")
        if choice == List[temp].ans: LBc.config(bg='blue',fg='white')  
    if choice == '4': 
        LBd.config(bg="#a4abc1")
        if choice == List[temp].ans: LBd.config(bg='blue',fg='white')       
def update():
    qtk.set(List[number.get()-1].q)
    atk.set(List[number.get()-1].a)
    btk.set(List[number.get()-1].b)
    ctk.set(List[number.get()-1].c)
    dtk.set(List[number.get()-1].d)
    reply.set('答')
def refresh():
    LBa.config(bg='white',fg='black')
    LBb.config(bg='white',fg='black')
    LBc.config(bg='white',fg='black')
    LBd.config(bg='white',fg='black')
def prev():
    if number.get() == 1: number.set(504)
    else: number.set(number.get() - 1)
    update()
    refresh()    
def next():
    if number.get() == 504: number.set(1)
    else: number.set(number.get() + 1)
    update()
    refresh()
def explain():#自訂函數解釋
    etk.set(Explain[number.get()-1]) #函數PASS跳過etk.set(Explain[number.get()-1])
def m(event):
    try:
        update()
        refresh()
    except Exception:
        print('劉任昌碰到例外錯誤')

f = open('金融常識.txt','r',encoding='utf-8') #讀取504列的題目
read = f.readlines() #read = f.read()
f.close()#解答2024.txt
List = []
f = open('解答2024.txt','r',encoding='utf-8',errors='ignore') #讀取504列的題目
readexplain = f.readlines() #read = f.read()
f.close()#解答2024.txt
Explain = []
Answer=(4,4,3,4,3,2,3,3,4,2,4,4,4,3,4,2,3,1,1,2,1,2,3,3,1,3,4,4,3,4,1,4,3,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= read[i].find('(1)')#讀題目的第1
    b2 = read[i].find('(2)')#讀題目的第2
    c3 = read[i].find('(3)')
    d4 = read[i].find('(4)')
    q = read[i][:a1]
    a = '(1) ' + read[i][a1 + 3 : b2]
    b = '(2) ' + read[i][b2 + 3 : c3]
    c = '(3) ' + read[i][c3 + 3 : d4]
    d = '(4) ' + read[i][d4 + 3 : -1]
    List.append( Financial( i+1, q, a, b, c, d, str(Answer[i])) )
    Explain.append(readexplain[i])

tk = tkinter.Tk()
tk.title('超級大梅女Python tkinter GUI金融常識測驗App')
tk.geometry('1200x400')
number = tkinter.IntVar(tk)
reply, qtk = tkinter.StringVar(tk), tkinter.StringVar(tk)
atk, btk = tkinter.StringVar(tk), tkinter.StringVar(tk)
ctk, dtk = tkinter.StringVar(tk), tkinter.StringVar(tk)
etk = tkinter.StringVar(tk)
select = ('1', '2', '3', '4')
reply.set('答')
number.set(1)
update()

option = tkinter.OptionMenu(tk, reply, *select, command=check).grid(column=1, row=0)
LBq = tkinter.Label(tk, textvariable=qtk, wrap=1000, justify="left",font=('標楷體',30),bg='pink')#題目
LBq.grid(column=2, row=0, sticky=tkinter.W)
LBa = tkinter.Label(tk, textvariable=atk, bg='white', fg='black')
LBa.grid(column=2, row=1, sticky=tkinter.W)                         #選項(1)
LBb = tkinter.Label(tk, textvariable=btk, bg='white', fg='black')
LBb.grid(column=2, row=2, sticky=tkinter.W)                         #選項(2)
LBc = tkinter.Label(tk, textvariable=ctk, bg='white', fg='black')
LBc.grid(column=2, row=3, sticky=tkinter.W)                         #選項(3)
LBd = tkinter.Label(tk, textvariable=dtk, bg='white', fg='black')
LBd.grid(column=2, row=4, sticky=tkinter.W)                         #選項(4)
LBe = tkinter.Label(tk, textvariable=etk, bg='white', fg='black', font=('微軟中黑體',16))
LBe.grid(column=2, row=5, sticky=tkinter.W)
LBnumber = tkinter.Entry(tk, textvariable=number, width = 4,font=("Arial", 20),bg="#377a37",fg='white')
LBnumber.grid(column=0, row=0)
LBnumber.bind('<Return>',m)#按下Return去執行m 小於<大於>
btnprev = tkinter.Button(tk, text='前一題', width=6, command=prev).grid(column=0, row=1)
btnnext = tkinter.Button(tk, text='下一題', width=6, command=next).grid(column=0, row=2)
btnEXPL = tkinter.Button(tk, text='大帥哥說明', width=10, command=explain, bg='yellow', fg='green',font=('微軟中黑體',16)).grid(column=0, row=3)
tk.mainloop()  #按下上面的btnEXPL則執行explain

留言

  1. https://chenyouhong.blogspot.com/2025/11/611tryexcept.html

    回覆刪除
  2. https://xieyouting.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  3. https://linyouzhen.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  4. https://yininglee96.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  5. https://zhanyujin.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  6. https://chiiu-shih-min.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  7. https://tzuchihsu.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  8. https://zhengword.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  9. https://qxrlybn.blogspot.com/2025/11/import-tkinter-tkinter-602-class_23.html

    回覆刪除
  10. 作者已經移除這則留言。

    回覆刪除
  11. https://umyjmyju45545.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  12. https://zhangzhixuan2006.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  13. https://wutsungchuan.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  14. https://kuowz.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  15. https://hejaijun.blogspot.com/2025/11/pythontryexpect.html

    回覆刪除
  16. https://aster1210.blogspot.com/2025/11/blog-post_24.html

    回覆刪除
  17. https://hidhfcaha.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  18. https://chunyuan14.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  19. https://11417155.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  20. https://lusiying5.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  21. https://hodkrbcjlkwsnd.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  22. https://yojheyu.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  23. https://eastyi.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  24. https://yangyuhsuan.blogspot.com/2025/11/pythontryexcept.html

    回覆刪除
  25. https://chenpaojen136.blogspot.com/2025/11/tryexcept.html

    回覆刪除
  26. https://zhangjingyan.blogspot.com/2025/11/import-tkinter-tkinter-602-class_24.html

    回覆刪除
  27. https://tsaiziytang.blogspot.com/2025/11/blog-post_24.html

    回覆刪除
  28. https://chrissie0209.blogspot.com/2025/11/blog-post_24.html

    回覆刪除
  29. https://andy20051217.blogspot.com/2025/11/pyhontkintermath.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

劉任昌python全域變數global和判斷if

甲班

劉任昌金融常識互動測驗PYTHON產生HTML,CSS,JAVASCRIPT