上一篇博文我们实现了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()