0

I am working on a project. I set a toplevel, a listbox and textbox on the toplevel. I want to have a function that is called when I click on one of the elements of the list, and then I want to get the index of the selected element, and update the textbox. Any ideas?

This is the code I have, but it doesn't do what I want it to do:

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

2 Answers 2

1

In your case, you can use lambda to pass the Listbox and Text widgets to the function:

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

Modify theFunction():

def theFunction(listbox, textbox):
    index = listbox.curselection()[0]
    value = listbox.get(index)
    textbox.insert(tk.END, str(value)+'\n')
    textbox.see(tk.END)
Sign up to request clarification or add additional context in comments.

Comments

1

Typically, I'll put my tkinter objects into a class. It makes it much easier to share objects between functions. Here is an example of how that could be achieved with the sample code you provided.

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

Additionally I changed your function binding from <Button-1> to <<ListboxSelect>>. I believe that one has the functionality you desired.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.