我正在构建一个gui,它从用户那里获取query
并运行leFunction.aFunction(query)
以返回名为Result
的列表。当用户按Enter键时,我希望每个标签上的文本都设置为Result
元素。我的尝试,如下所示,只打印出最后一个标签上Result
的最后一个元素......(跟我说话,我是Tkinter的初学者。)
谢谢。
import Tkinter
import os
import leFunction
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(u"")
for j in range (1,10):
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable,
anchor="w",fg="black",bg="white")
label.bind("<1>", lambda event, text="text": \
self.click_link(event, text))
label.grid(column=0,row=j,columnspan=2,sticky='EW')
self.labelVariable.set(j)
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
#Result is a list
Result = leFunction.aFunction(self.entryVariable.get())
for i in range (0,len(Result)):
print "result",i,":",Result[i]
self.labelVariable.set(Result[i])
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def click_link(self, event, text):
print "you clicked '%s'" % text
#os.system("open "+file)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()
答案 0 :(得分:1)
以下代码有效(假设您的aFunction
代码按预期工作):
import Tkinter
import os
import leFunction
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(u"")
self.labelVars = []
for j in range (1,10):
labelVar = Tkinter.StringVar()
labelVar.set(j)
self.labelVars.append(labelVar)
label = Tkinter.Label(self,textvariable=labelVar,
anchor="w",fg="black",bg="white")
label.bind("<1>", lambda event, text="sup": \
self.click_link(event, text))
label.grid(column=0,row=j,columnspan=2,sticky='EW')
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
#Result is a list
Result = leFunction.aFunction(self.entryVariable.get())
for i in range (0,len(Result)):
print "result",i,":",Result[i]
self.labelVars[i].set(Result[i])
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def click_link(self, event, text):
print "you clicked '%s'" % text
#os.system("open "+file)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()
您现在不是一遍又一遍地设置self.labelVariable
,而是创建一个self.labelVars
列表,每个元素对应一个labelVar
。然后,您可以像我一样设置此列表元素的文本:
self.labelVars[i].set(Result[i])