Python tkinter get()没有属性
我想在Python tkinter get()没有属性,python,tkinter,Python,Tkinter,我想在条目小部件中使用列表中的每个变量,并在按下按钮后获取文本 self.input_text = ['l1', 'l2', 'l3'] self.activeRow = 3 for e in self.input_text: e = StringVar() t = Entry(textvariable=e) t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W) self.activeRow += 1
条目
小部件中使用列表中的每个变量,并在按下按钮后获取文本
self.input_text = ['l1', 'l2', 'l3']
self.activeRow = 3
for e in self.input_text:
e = StringVar()
t = Entry(textvariable=e)
t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W)
self.activeRow += 1
最后,我想将列表中每个变量的值写入一个文件,但现在,我似乎无法在按下按钮后打印self.input_text[0]
button1 = Button(text='Write',command=self.__writeNewInfo, width = 15)
button1.grid(row=self.activeRow,column=5,sticky=W)
def __writeNewInfo(self):
x = self.input_text[0].get()
y = self.input_text[1].get()
z = self.input_text[2].get()
print x
print y
print z
创建
StringVar
的实例时,您做了错误的事情。将e
设置为列表中的项目,然后将其覆盖为空的StringVar
。相反,请尝试:
for i, e in enumerate(self.input_text):
self.input_text[i] = StringVar() #Overwrite this item in the list
self.input_text[i].set(e) #Set the StringVar to take the value from the list
t = Entry(textvariable=self.input_text[i])
t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W)
self.activeRow += 1
创建
StringVar
的实例时,您做了错误的事情。将e
设置为列表中的项目,然后将其覆盖为空的StringVar
。相反,请尝试:
for i, e in enumerate(self.input_text):
self.input_text[i] = StringVar() #Overwrite this item in the list
self.input_text[i].set(e) #Set the StringVar to take the value from the list
t = Entry(textvariable=self.input_text[i])
t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W)
self.activeRow += 1
self.input_文本基本上是一个字符串列表。如果要从条目中检索某些内容,则需要保留该条目的变量并对其调用get() 例如:
self.input_text = ['l1', 'l2', 'l3']
self.entries = []
self.activeRow = 3
for e in self.input_text:
e = StringVar()
t = Entry(textvariable=e)
t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W)
self.activeRow += 1
self.entries.append(t)
button1 = Button(text='Write',command=self.__writeNewInfo, width = 15)
button1.grid(row=self.activeRow,column=5,sticky=W)
def __writeNewInfo(self):
x = self.entries[0].get()
y = self.entries[1].get()
z = self.entries[2].get()
print x
print y
print z
self.input_文本基本上是一个字符串列表。如果要从条目中检索某些内容,则需要保留该条目的变量并对其调用get() 例如:
self.input_text = ['l1', 'l2', 'l3']
self.entries = []
self.activeRow = 3
for e in self.input_text:
e = StringVar()
t = Entry(textvariable=e)
t.grid(row=self.activeRow,column=4,columnspan=2,sticky=W)
self.activeRow += 1
self.entries.append(t)
button1 = Button(text='Write',command=self.__writeNewInfo, width = 15)
button1.grid(row=self.activeRow,column=5,sticky=W)
def __writeNewInfo(self):
x = self.entries[0].get()
y = self.entries[1].get()
z = self.entries[2].get()
print x
print y
print z
那么,您想创建几个
条目
小部件,用列表
中的字符串填充它们的输入字段,然后检索包含的字符串?那么,您想创建几个条目
小部件,用列表
中的字符串填充它们的输入字段,然后检索包含的字符串?