1

I'm having this little nasty problem for which i'm afraid the answer is just to easy to think about in python 3 with tkinter.

So i'm making this on-screen-keyboard which I want to be enabled whenever I push the "enterCountryBtn", however it only enables one button/"key" from the kybrd_list.

The question is simple; How can I make sure that the complete list is enabled whenever i push "enterCountryBtn"?

This is the part of code where my problems seem to occur:

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)

thanks in advance, Niels

4

1 回答 1

1

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

这是一个可运行的示例:

import Tkinter as tk
kybrd_list = ['q','w','e','r','t','y','...']
def onclick(k):
    def click():
        print(k)
    return click

class SimpleGridApp(object):
    def __init__(self, master, **kwargs):
        self.keyboardButtons = []
        for i, k in enumerate(kybrd_list):
            button = tk.Button(root, text=k, width=5, relief='raised',
                               command=onclick(k), state=tk.DISABLED)
            button.grid(row=i, column=0)
            self.keyboardButtons.append(button)

        self.enterCountryBtn = tk.Button(
            root, width=30, text="enter Country", command=self.countryCommand)
        self.enterCountryBtn.grid(row=7, column=0)            
    def countryCommand(self):
        for button in self.keyboardButtons:
            button['state']=tk.NORMAL

root = tk.Tk()
app = SimpleGridApp(root, title='Hello, world')
root.mainloop()
于 2013-05-27T16:57:56.677 回答