Python Forum
open file from listbox to Text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
open file from listbox to Text
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open a setup python file? Adlo777 2 1,920 Jan-28-2021, 08:10 PM
Last Post: nilamo
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,583 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Convert text from an image to a text file Evil_Patrick 5 4,305 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,460 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020