我正在做一个项目。我在顶层设置了一个顶层,一个列表框和文本框。我希望在单击列表中的一个元素时调用一个函数,然后获取所选元素的索引,并更新文本框。有什么想法吗
这是我的代码,但它不符合我的要求:
def helpInitialize():
help = Toplevel()
help.title("Help")
help.geometry("1000x650")
help.resizable(0,0)
helpFiles= []
listy = Scrollbar(help)
listy.place(x=143, y=20, height=565)
listHelp = Listbox(help, height=35, width=20)
listHelp.place(x=20, y=20)
listHelp.config(yscrollcommand=listy.set)
listy.config(command=listHelp.yview)
texty = Scrollbar(help)
texty.place(x= 977, y= 20, height = 565)
textx = Scrollbar(help,orient= HORIZONTAL)
textx.place(x = 175, y= 583, width = 800)
helpText = Text(help, bg="white", height=35, width=100)
helpText.place(x=175, y=20)
helpText.configure(state="disabled")
helpText.config(yscrollcommand=texty.set, xscrollcommand=textx.set)
texty.config(command=helpText.yview)
textx.config(command=helpText.xview)
listHelp.bind("<Button-1>", theFunction)
def theFunction(event):
print(listHelp.get(listHelp.curselection()))
在您的情况下,可以使用
lambda
将Listbox
和Text
小部件传递给函数:修改
theFunction()
:通常,我会将tkinter对象放入一个类中。它使函数之间共享对象变得更加容易。下面是一个如何使用您提供的示例代码实现的示例
此外,我将函数绑定从
<Button-1>
更改为<<ListboxSelect>>
。我相信它具有您想要的功能相关问题 更多 >
编程相关推荐