Python GUI 2048的圖形界面實現(二)

       上一篇博文我們實現了2048的界面設計,這一篇我們繼續。

       上一篇的界面中,button的觸發事件並沒有定義,所以這一次我們要加入button觸發的事件定義。label顯示的值也是我們定義時寫進去的定值,這一次要根據button事件觸發,改變相應的lebal值。

        當然我們還要定義一個二維數組,然後把數組對應的值給對應的lebal,button觸發後,改變相應的數組值,從而改變lebal的值。

       好,我們開始,定義一個二維數組:

       mtr = [[1 for i in range(4)] for j in range(4)]

       這樣,一個4*4的數組就定義好了,它的16個值都是1。

       然後把數組裏的值,給到對應的標籤:

       helloLabel = Label(win, textvariable=mt00, height=2, width=10)
       helloLabel.grid(row=0, column=0)

       mt00=IntVar() 
       mt00.set(mtr[0][0])

       然後定義button的觸發事件:

      button_up = Button(win, text='UP', height=2, width=10,command=click_up)
      button_up.grid(row=5, column=1)

      def click_up(): 
      mtr[0][0]=3
       mt00.set(mtr[0][0])

      我們簡單梳理下,Button屬性裏command=click_up,觸發click_up函數,在click_up函數中,我們改變了二維數組第一行第一列的值,賦給mt00。

     然後在標籤中,textvarable 也就是標籤要顯示的動態內容,是指向mt00的,這就實現了,點擊button時,動態改變標籤的值。

     下面貼代碼:

from Tkinter import *
def click_back():  
    mtr[0][1]=3
    mt01.set(mtr[0][1]) 
def click_quit():  
    mtr[0][2]=3
    mt02.set(mtr[0][2])
def click_up():  
    mtr[0][0]=3
    mt00.set(mtr[0][0])
def click_left():  
    mtr[0][3]=3
    mt03.set(mtr[0][3])
def click_down():  
    mtr[1][0]=3
    mt10.set(mtr[1][0])
def click_right():  
    mtr[2][0]=3
    mt20.set(mtr[2][0]) 
root = Tk()

win = Frame(root, height=600, width=600)
win.grid_propagate(False)
win.grid()


mtr = [[1 for i in range(4)] for j in range(4)]
mt00=IntVar()  
mt00.set(mtr[0][0])
mt01=IntVar()  
mt01.set(mtr[0][1])
mt02=IntVar()  
mt02.set(mtr[0][2])
mt03=IntVar()  
mt03.set(mtr[0][3])
mt10=IntVar()  
mt10.set(mtr[1][0])
mt11=IntVar()  
mt11.set(mtr[1][1])
mt12=IntVar()  
mt12.set(mtr[1][1])
mt13=IntVar()  
mt13.set(mtr[1][3])
mt20=IntVar()  
mt20.set(mtr[2][0])
mt21=IntVar()  
mt21.set(mtr[2][1])
mt22=IntVar()  
mt22.set(mtr[2][2])
mt23=IntVar()  
mt23.set(mtr[2][3])
mt30=IntVar()  
mt30.set(mtr[3][0])
mt31=IntVar()  
mt31.set(mtr[3][1])
mt32=IntVar()  
mt32.set(mtr[3][2])
mt33=IntVar()  
mt33.set(mtr[3][3])

helloLabel = Label(win, textvariable=mt00, height=2, width=10)
helloLabel.grid(row=0, column=0)
helloLabe2 = Label(win, textvariable=mt01, height=2, width=10,bg='blue')
helloLabe2.grid(row=0, column=1)
helloLabe3 = Label(win, textvariable=mt02, height=2, width=10)
helloLabe3.grid(row=0, column=2)
helloLabe4 = Label(win, textvariable=mt03, height=2, width=10)
helloLabe4.grid(row=0, column=3)
helloLabe5 = Label(win, textvariable=mt10, height=2, width=10,bg='black',fg='white')
helloLabe5.grid(row=1, column=0)
helloLabe6 = Label(win, textvariable=mt11, height=2, width=10)
helloLabe6.grid(row=1, column=1)
helloLabe7 = Label(win, textvariable=mt12, height=2, width=10)
helloLabe7.grid(row=1, column=2)
helloLabe8 = Label(win, textvariable=mt13, height=2, width=10,bg='red')
helloLabe8.grid(row=1, column=3)
helloLabe9 = Label(win, textvariable=mt20, height=2, width=10)
helloLabe9.grid(row=2, column=0)
helloLabe10 = Label(win, textvariable=mt21, height=2, width=10)
helloLabe10.grid(row=2, column=1)
helloLabe11 = Label(win, textvariable=mt22, height=2, width=10,bg='red')
helloLabe11.grid(row=2, column=2)
helloLabe12 = Label(win, textvariable=mt23, height=2, width=10)
helloLabe12.grid(row=2, column=3)
helloLabe13 = Label(win, textvariable=mt30, height=2, width=10)
helloLabe13.grid(row=3, column=0)
helloLabe14 = Label(win, textvariable=mt31, height=2, width=10)
helloLabe14.grid(row=3, column=1)
helloLabe15 = Label(win, textvariable=mt32, height=2, width=10)
helloLabe15.grid(row=3, column=2)
helloLabe16 = Label(win, textvariable=mt33, height=2, width=10,bg='green')
helloLabe16.grid(row=3, column=3)

helloLabel_step = Label(win, text='0', height=2, width=10,bg='red')
helloLabel_step.grid(row=7, column=1)
helloLabel_score = Label(win, text='0', height=2, width=10,bg='red')
helloLabel_score.grid(row=7, column=3)
button_back = Button(win, text='BACK', height=2, width=10,command=click_back)
button_back.grid(row=4, column=0)
button_quit = Button(win, text='QUIT', height=2, width=10,command=click_quit)
button_quit.grid(row=4, column=2)
button_up = Button(win, text='UP', height=2, width=10,command=click_up)
button_up.grid(row=5, column=1)
button_left = Button(win, text='LEFT', height=2, width=10,command=click_left)
button_left.grid(row=6, column=0)
button_down = Button(win, text='DOWN', height=2, width=10,command=click_down)
button_down.grid(row=6, column=1)
button_right = Button(win, text='RIGTH', height=2, width=10,command=click_right)
button_right.grid(row=6, column=2)
_step = Label(win, text='STEP:', height=2, width=10)
_step.grid(row=7, column=0)
_score = Label(win, text='SCORE:', height=2, width=10)
_score.grid(row=7, column=2)
root.mainloop()


 

 

 

     

      

      

      

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章