Python和Tkinter,将列表信息从一个函数传递到另一个函数

2024-06-16 22:17:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目。我在顶层设置了一个顶层,一个列表框和文本框。我希望在单击列表中的一个元素时调用一个函数,然后获取所选元素的索引,并更新文本框。有什么想法吗

这是我的代码,但它不符合我的要求:

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()))

Tags: confighelpplacewidthcommandheight文本框set
2条回答

在您的情况下,可以使用lambdaListboxText小部件传递给函数:

listHelp.bind('<Button-1>', lambda e: theFunction(e.widget, helpText))

修改theFunction()

def theFunction(listbox, textbox):
    index = listbox.curselection()[0]
    value = listbox.get(index)
    textbox.insert(tk.END, str(value)+'\n')
    textbox.see(tk.END)

通常,我会将tkinter对象放入一个类中。它使函数之间共享对象变得更加容易。下面是一个如何使用您提供的示例代码实现的示例

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()

此外,我将函数绑定从<Button-1>更改为<<ListboxSelect>>。我相信它具有您想要的功能

相关问题 更多 >