我一直在尝试获取一个条目值(代码中的 S1)以将其自身设置为一个值(_attributes 字典中的 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()
新代码(似乎正在工作,至少我得到了我想要的东西)。我将不得不稍微修改它以使其成为顶部循环,但这是基础
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__