我正在爲python掃雷遊戲編寫一個菜單,該選項允許玩家輸入自定義的行,列和我的計數。我想這樣做是爲了點擊簡單,中等或硬單選按鈕將禁用所有三個輸入小部件,並且單擊自定義單選按鈕將啓用所有三個小部件。使用tkinter.grid啓用/禁用多個輸入小部件()
我的代碼:
from tkinter import *
def customDisable():
E_row.configure(state='disabled')
E_row.update()
E_col.configure(state='disabled')
E_col.update()
E_mine.configure(state='disabled')
E_mine.update()
def customEnable():
E_row.configure(state='normal')
E_row.update()
E_col.configure(state='normal')
E_col.update()
E_mine.configure(state='normal')
E_mine.update()
menu=Tk()
menu.title('Pysweeper V 1.0')
ms=Canvas(menu,width=5,height=5)
ms.data={}
menu.resizable(0,0)
T_row=Label(menu,text='Rows:',anchor=E).grid(row=1,column=2)
T_col=Label(menu,text='Columns:',anchor=E).grid(row=2,column=2)
T_mine=Label(menu,text='Mines:',anchor=E).grid(row=3,column=2)
E_row=Entry(menu,state='normal').grid(row=1,column=3)
E_col=Entry(menu,state='normal').grid(row=2,column=3)
E_mine=Entry(menu,state='normal').grid(row=3,column=3)
dif=IntVar()
RB_easy=Radiobutton(menu, text='Easy',anchor=W,variable=dif,value=1,command=customDisable).grid(row=1,column=1,rowspan=2)
RB_med=Radiobutton(menu, text='Medium',anchor=W,variable=dif,value=2,command=customDisable).grid(row=3,column=1,rowspan=2)
RB_hard=Radiobutton(menu, text='Hard',anchor=W,variable=dif,value=3,command=customDisable).grid(row=5,column=1,rowspan=2)
RB_cust=Radiobutton(menu, text='Custom game',variable=dif,value=4,command=customEnable).grid(row=7,column=1)
執行代碼給了我,沒有錯誤如下窗口:http://i.imgur.com/aNItO0f.png
然而,當我選擇一個單選按鈕,控制檯打印錯誤:
看來,這個錯誤是由於沒有傳入E_row值而引起的,但我已經獲得了使用.pack()而不是網格工作的方法。通常我只需切換到.pack();然而,由於我的遊戲其餘部分是.grid()而不是.pack(),所以我仍然使用.grid作爲菜單。我在這裏錯過了什麼?
甚至更好:將網格語句分組在一起,以便更容易地可視化佈局。 – 2014-04-21 20:49:49