Python Forum
open file from listbox to Text - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: open file from listbox to Text (/thread-23879.html)



open file from listbox to Text - elstolbo - Jan-21-2020

hello programmers. I would like advice on opening a file from a listbox and reading it in the Text box. I have a listbox in which I have listed all the files that are in a certain folder and I need to open them in the Text box, which is right next to the listbox.
I created the code on windows, but when I need to put it on raspberry pi, it doesn't work.
I attach the code.
def report():
        global listbox
        ctr_left.grid_slaves
        ctr_right.grid_slaves

        report_left = Frame(ctr_left, width=200, height=360, padx=5, pady=5)
        report_left.grid(row=0, column=0, sticky="nsew")

        scroll = Scrollbar(report_left)
        scroll.pack(side=RIGHT, fill=Y)

        listbox = Listbox(report_left, width=90, height=20, yscrollcommand=scroll.set)
        listbox.pack(side=LEFT)

        flist = os.listdir("C:\\Users\\stolb\\Desktop\\sracky\\")
        path = ("C:\\Users\\stolb\\Desktop\\sracky\\")
        for item in flist:
            listbox.insert(END, item)


        report_right = Frame(ctr_right, width=600, height=360, padx=5, pady=5)
        report_right.grid(row=0, column=0, sticky="nsew")

        def showcontent(event):
                x = listbox.curselection()[0]
                file = listbox.get(x)
                with open(path+file, 'r', encoding="utf-8") as file:
                        file = file.read()
                text.delete('1.0', END)
                text.insert(END, file)
        text = Text(report_right)
        text.pack()
        listbox.bind("<<ListboxSelect>>", showcontent)
of course, I replaced the path with another.
I think there is a bug in listbox.bind ("<<ListboxSelect>>"), showcontent) because my application in which I do this is on raspberry pi with a 7 inch monitor and I feel it doesn't work to the touch. After clicking on the file, it does not refer to the showcontent function, which lists the contents of the file.
If you know what could be a mistake, please advise me, thank you