<p>通常,我会将tkinter对象放入一个类中。它使函数之间共享对象变得更加容易。下面是一个如何使用您提供的示例代码实现的示例</p>
<pre><code>from tkinter import Toplevel, Scrollbar, Listbox, Text, HORIZONTAL, END
class Help:
def __init__(self):
self.help = Toplevel()
self.help.title("Help")
self.help.geometry("1000x650")
self.help.resizable(0,0)
self.helpFiles = ['Test ' + str(i) for i in range(100)]
self.listy = Scrollbar(self.help)
self.listy.place(x=143, y=20, height=565)
self.listHelp = Listbox(self.help, height=35, width=20)
self.listHelp.place(x=20, y=20)
self.listHelp.config(yscrollcommand=self.listy.set)
self.listy.config(command=self.listHelp.yview)
for item in self.helpFiles:
self.listHelp.insert(END, item)
self.texty = Scrollbar(self.help)
self.texty.place(x= 977, y= 20, height = 565)
self.textx = Scrollbar(self.help, orient= HORIZONTAL)
self.textx.place(x = 175, y= 583, width = 800)
self.helpText = Text(self.help, bg="white", height=35, width=100)
self.helpText.place(x=175, y=20)
self.helpText.configure(state="disabled")
self.helpText.config(yscrollcommand=self.texty.set, xscrollcommand=self.textx.set)
self.texty.config(command=self.helpText.yview)
self.textx.config(command=self.helpText.xview)
self.listHelp.bind("<<ListboxSelect>>", self.theFunction)
def theFunction(self, event):
print(self.listHelp.get(self.listHelp.curselection()))
# your code here
if __name__ == '__main__':
test = Help()
test.help.mainloop()
</code></pre>
<p>此外,我将函数绑定从<code><Button-1></code>更改为<code><<ListboxSelect>></code>。我相信它具有您想要的功能</p>