張宇辰輸入tkinter繪圖模組建立金融常識測驗app

  <PRE STYLE='BORDER: 10PX GREEN DASHED'>

   <IFRAME WIDTH="100%" HEIGHT='400' SRC="https://zh.wikipedia.org/zh-tw/Tkinter"></IFRAME>

   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()

    

f = open('金融常識.txt','r',encoding='utf-8')

read = f.readlines() #read = f.read()

f.close()

List = []

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,2,4,2,2,3,2,4,1,4,1,1,4,3,1,2,4,1,2,1,4,4,4,1,4,2,1,3,3,1,4,4,3,2,2,2,4,4,1,3,3,1,2,4,1,3,4,4,2,4,4,3,4,1,3,2,3,2,3,2,2,2,4,4,4,1,1,2,2,4,4,1,4,1,4,4,4,2,1,1,2,1,2,1,2,2,3,3,3,4,2,1,1,1,1,2,2,1,4,4,4,4,4,4,4,3,4,4,1,1,2,2,3,4,4,3,1,4,2,3,3,2,1,3,4,4,2,4,1,1,3,3,2,2,3,1,2,1,4,3,3,2,2,2,1,4,2,4,3,3,3,1,3,3,2,3,3,1,4,4,3,2,4,2,2,4,1,1,1,3,1,3,2,2,3,4,4,3,2,2,1,2,4,1,1,2,2,1,1,1,3,1,2,2,2,1,1,4,1,3,1,1,1,1,1,3,1,2,3,3,3,2,4,3,3,3,1,2,4,2,3,3,1,4,2,3,3,3,4,1,2,3,2,3,4,2,3,4,4,1,2,4,3,4,4,2,2,3,1,4,3,4,2,1,2,3,2,4,2,1,3,4,1,2,1,1,4,1,2,4,1,4,3,4,4,2,2,1,1,2,3,4,3,3,1,3,1,4,4,3,2,4,4,2,1,1,4,1,1,2,3,2,3,3,4,2,2,4,3,3,3,2,1,1,2,2,4,1,4,1,3,1,1,4,1,4,4,3,1,4,3,3,2,1,1,4,2,4,2,4,1,1,4,1,3,1,2,4,1,1,4,1,3,1,3,4,1,4,2,3,1,3,2,3,3,3,4,2,3,4,3,1,4,1,3,3,4,4,4,4,1,2,3,1,3,3,3,1,3,4,1,3,1,4,1,3,2,4,4,3,1,1,4,2,1,3,1,2,2,1,1,4,3,2,3,4,1,4,1,1,4,2,1,2,1,2,4,3,1,3,3,2,4,3,1,2,1,3,3,3,1,4,3,1,3,2,3,1,3,1,4,1,1,4,4,2,3,1,4,4,3,4,1,3,4,2,3,4,4,2,3,1)

nList, nAnswer = len(read), len(Answer) 

if nList != nAnswer: print('題目長度 ', nList, ' 與答案長度 ', nAnswer,' 不同!')

for i in range( nList ):

    a1 = read[i].find('(1)')

    b2 = read[i].find('(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])) )


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)

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,font=('微軟中黑體',20), justify="left")#題目

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)

LBnumber = tkinter.Entry(tk, textvariable=number, width = 3)

LBnumber.grid(column=0, row=0)

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)

btnExplain = tkinter.Button(tk, text='解答', width=6, command=next).grid(column=0, row=3)

tk.mainloop()

   </PRE>

留言

  1. 順利執行!但你考卷留的網址錯誤 ycuhenzhanggg. https://yuchenzhangggg.blogspot.com/2025/10/h1background-color-purple-color-white.html
    tkinter. https://yuchenzhangggg.blogspot.com/2025/10/tkinterapp_13.html
    第三次上課. http://yuchenzhangggg.blogspot.com/2025/10/tkinterapp.html
    第二次上課.https://yuchenzhangggg.blogspot.com/2025/09/pythonfind.html
    第一次上課.https://yuchenzhangggg.blogspot.com/2025/09/pythoncountsplit.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

張宇辰python字串方法count與split

張宇辰PYTHON字串find字串