Home > other >  How to have a screen that lists multiple results, each of which is a clickable link (on Tkinter)
How to have a screen that lists multiple results, each of which is a clickable link (on Tkinter)

Time:01-10

I'm new to GUI's, I made a basic search engine that finds documents that contain certain words and puts them in a list.

Now, I want this list of paths to be displayed on my GUI, one under the other (using \n I guess), all of them clickable and automatically opening the right document for you with something like: os.startfile(path, 'open')

In the current version I am only displaying one result (the first in the list) and I'm doing it with a label as so:

my_label.config(text=path)
my_label.bind("<Button-1>", lambda e: os.startfile(path, 'open'))

I could just make more labels but then it's inefficient and also not dynamic (the one I envision would list all results and be scrollable for example).

Appreciate any help in this.

CodePudding user response:

You can use a set of Label and bind right click to it, or a single Listbox to show your paths and bind on it.

from tkinter import *
import os

root = Tk()

lst = [f'path {i}' for i in range(1,6)]

def select(e):
    path = e.widget.get(*e.widget.curselection())
    os.startfile(path,'open')

lstbox = Listbox(root)
lstbox.pack(padx=10,pady=10)

for i in lst:
    lstbox.insert('end',i)

lstbox.bind('<<ListboxSelect>>',select) # Or lstbox.bind('<Double-1>',select) for double click

root.mainloop()

lst is supposed to be replaced with your required list. Another viable approach is set of Buttons with command that opens the required path.

  •  Tags:  
  • Related