tkinter启用按钮列表,但只有一个(最后一个)响应

2024-05-18 07:14:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个小麻烦,恐怕在python3和tkinter中,答案很容易想到。在

所以我在做这个屏幕键盘,我想在我按下“enterCountryBtn”时启用它,但是它只启用kybrd_列表中的一个按钮/“key”。在

问题很简单;当我按下“enterCountryBtn”时,如何确保启用完整列表?在

我的问题似乎出现在代码的这一部分:

def countryCommand():
    keyboardButtons['state']=tk.NORMAL
    print("country")


kybrd_list = [
'q','w','e','r','t','y','...']

ro = 2
co = 0

for k in kybrd_list:
     *A bunch of stuff goes here*
     keyboardButtons=tk.Button(root, text=k, width=5, relief=rel2, command=cmd2, state=tk.DISABLED
     *and some more stuff here*

enterCountryBtn = tk.Button(root, width=30, text="enter Country", command=countryCommand)
enterCountryBtn.grid(row=7, column=0)

提前谢谢你, 尼尔斯


Tags: text列表herebuttonrootwidthcommandtk
1条回答
网友
1楼 · 发布于 2024-05-18 07:14:44

列出keyboardButtons一个列表:

def countryCommand():
    for button in keyboardButtons:
        button['state']=tk.NORMAL

keyboardButtons = []
for k in kybrd_list:
    ...
    keyboardButtons.append(tk.Button(root, text=k, width=5, relief=rel2, command=cmd2, state=tk.DISABLED))
    ...

下面是一个可运行的示例:

^{pr2}$

相关问题 更多 >