我一直试图获取一个条目值(代码中的S1)来将自己设置为一个值(STR在属性字典中),但我就是无法让它工作。我想让它成为一个最终的toploop,但我在这个问题上要一步一步地做,因为我对编程还不太熟悉。我这样做是正确的,还是应该有一个按钮,当按下时,对输入值进行一次查找,然后继续查找?我在网上为Tkinter找到了好几本教程和课程,但似乎离我期望的工作方式还有几英里远。

#! usr/bin/python27
from Tkinter import *

class Character:

    def __init__(self, **kvargs):
        self._attributes = kvargs

    def set_attributes(self, key, value):
        self._attributes[key] = value
        return

    def get_attributes(self, key):
        return self._attributes.get(key, None)


def attrInput(stat, x, y):
   """Creates a label for entry box"""
   L = Label(B,
             width = 5,
             relief = RIDGE,
             anchor = E,
             text = stat).grid(row = x,
                             column = y)

B = ""
def main():

    Person = Character()

    B = Tk()

    S1 = Entry(B, width = 3)
    S1.grid(row = 0, column = 1)
    S1.bind("<Key>", Person.set_attributes('STR', S1.get()) )
    attrInput("Str: ", 0, 0)

    Button(B, text='Quit', command=B.destroy).grid(row=3, column=0, sticky=W, pady=4)

    B.mainloop()

    print Person.__dict__

if __name__ == '__main__': main()

新代码(似乎正在工作,至少我已经从中得到了我想要的东西)。我得稍微修改一下,让它成为一个Topopp,但是这里是基础。
class Character:

    def __init__(self, **kvargs):
        self._attribute = kvargs

    def set_attribute(self, key, value):
        self._attribute[key] = value
        return

    def get_attribute(self, key):
        return self._attribute.get(key, None)


class attrAsk:

    def __init__(self, master, Char, attrName, Row, Column):
        self.Char = Char
        self.attrName = attrName
        attrInput(attrName+":", Row, Column)
        self.e = Entry(master, width = 3)
        self.e.grid(row = Row, column = Column+1)
        self.e.bind("<KeyRelease>", self.set_attr)

    def set_attr(self, event):
        self.Char.set_attribute(self.attrName, self.e.get())


def attrInput(stat, x, y):
   """Creates a label for entry box"""
   L = Label(box,
             width = 5,
             relief = RIDGE,
             anchor = E,
             text = stat).grid(row = x,
                               column = y)

Person= Character()


box = Tk()

STRENT = attrAsk(box, Person, "STR", 0, 0)
DEXENT = attrAsk(box, Person, "DEX", 1, 0)
CONENT = attrAsk(box, Person, "CON", 2, 0)
INTENT = attrAsk(box, Person, "INT", 3, 0)
WISENT = attrAsk(box, Person, "WIS", 4, 0)
CHAENT = attrAsk(box, Person, "CHA", 5, 0)

Button(box,
       text='Continue',
       command=box.destroy).grid(columnspan = 2,
                                                       row=8,
                                                       column=0,
                                                       sticky=W,
                                                       pady=4)

box.mainloop()

print Person.__dict__

最佳答案

更改行:

S1.bind("<Key>", Person.set_attributes('STR', S1.get()) )

类似于:
def key_pressed(event):
    Person.set_attributes('STR', S1.get())
S1.bind("<KeyRelease>", key_pressed)

原始代码不起作用有两个原因:
bind将函数作为其第二个参数—然后在事件发生时调用该函数。然而,当你使用这个表达式时,它会立即出现。您需要将该表达式放入一个函数中,以便它只在按下键时发生。
Person.set_attributes('STR', S1.get())表示事件在第一次按下键时发生,但您希望在释放键时发生(因此添加了新字符)。因此,您需要使用<Key>
另一个注意事项:最好将所有功能(尤其是回调方法)组织到一个类中。例如:
class Window(object):
    def __init__(self):
        self.person = Character()

        self.B = Tk()

        self.S1 = Entry(B, width = 3)
        self.S1.grid(row = 0, column = 1)

        self.S1.bind("<KeyRelease>", self.key_pressed)
        attrInput("Str: ", 0, 0)

        self.button = Button(B, text='Quit', command=self.B.destroy).grid(row=3, column=0, sticky=W, pady=4)

        self.B.mainloop()

        print self.person.__dict__

    def key_pressed(self, event):
        self.person.set_attributes('STR', self.S1.get())


def main():
    w = Window()


if __name__ == '__main__': main()

这个组织的好处可能不是很明显,但是一旦拥有大量回调方法并跟踪大量小部件,它就会变得非常有用。
为了响应您的注释,您可以在for循环中创建<KeyRelease>Entry对象,每个对象都在自己的行上。然后,Label方法可以从传递给它的key_pressed对象中学习字段和输入文本,如下所示(尝试):
class Window(object):
    def __init__(self):
        self.person = Character()

        self.B = Tk()

        self.fields = ["STR", "DEX", "CON", "INT", "WIS", "CHA"]

        self.inputs = {}
        for i, f in enumerate(self.fields):
            self.inputs[f] = Entry(B, width = 3)
            self.inputs[f].grid(row=i, column=1)
            self.inputs[f].bind("<KeyRelease>", self.key_pressed)
            attrInput(f + ":", i, 0)

        self.button = Button(B, text='Quit', command=self.B.destroy).grid(row=7, column=0, sticky=W, pady=4)

        self.B.mainloop()

        print self.person.__dict__

    def key_pressed(self, event):
        field = self.fields[int(event.widget.grid_info()["row"])]
        self.person.set_attributes(field, event.widget.get())

关于python - python:获取Tkinter条目值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14411149/

10-11 00:59