我想打印标签文本(点击时FunctionResults
的元素)。
import Tkinter
import os
import Func
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
self.Result =[]
#self.minsize(8000,300)
self.geometry("800x300")
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,30):
labelVar = Tkinter.StringVar()
labelVar.set('')
self.labelVars.append(labelVar)
label = Tkinter.Label(self,textvariable=labelVar,
anchor="w",fg="black",bg="white")
label.bind("<1>", lambda event, text="some text": \
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):
#FunctionResults is a list returned by Func.Func1
self.FunctionResults = Func.Func1(self.entryVariable.get())
self.FunctionResults = list(reversed(self.Result))
for i in range (0,len( self.Result)):
print "result",i,":", self.FunctionResults[i]
self.labelVars[i].set(self.FunctionResults[i])
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def click_link(self, event, text):
print "you clicked '%s'" % text
#l["text"]
#os.system("open "+textvariable+".txt")
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()
相反,我得到you clicked some text
。
如何编写函数click_link(self, event, text):
以获取与单击标签相关联的名称?
我使用command
看到了类似主题的一些答案,但我不想使用按钮。
答案 0 :(得分:2)
您可以从点击的小部件中获取文本,如下所示:
def click_link(self, event, text):
print "you clicked '%s'" % event.widget["text"]
编辑在Ubuntu 14.04和OS X 10.10上为我工作的完整代码
import Tkinter
import os
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
self.Result =[]
#self.minsize(8000,300)
self.geometry("800x300")
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,30):
labelVar = Tkinter.StringVar()
labelVar.set('')
self.labelVars.append(labelVar)
label = Tkinter.Label(self,textvariable=labelVar,
anchor="w",fg="black",bg="white")
label.bind("<1>", lambda event, text="some text": \
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
self.Result = self.entryVariable.get()
self.Result = list(reversed(self.Result))
for i in range (0,len( self.Result)):
print "result",i,":", self.Result[i]
self.labelVars[i].set(self.Result[i])
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def click_link(self, event, text):
print "you clicked '%s'" % event.widget["text"]
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()