Reputation: 21
I am Working with the Listboxes in Python. I have inserted a list of books in the Listbox. But I wish to show the details of these books when I click the book names in the Listbox. I have tried using the if statement but it is not working.Here is the code
def det():
for val in my_listbox:
if val==b1:
print('It’s impossible to talk about mystery novels without immediately thinking of the legendary Agatha Christie. Amongst all of her works, none has a story quite as impeccably crafted as And Then There Were None, which explains why it is the best selling mystery book of all time.' )
where b1 is the first book in the listbox. Please suggest me some command which I can use.
Upvotes: 0
Views: 46
Reputation: 386342
The simplest solution -- assuming that each value in the listbox is unique -- is to associate a dictionary or database row with the listbox items. Then it becomes a trivial matter to show the data associated with the selection.
For example, given a data structure like this:
data = {
"Treasure Island": {"author": "Robert Louis Stevenson"},
"The Adventures of Tom Sawyer": {"author": "Mark Twain"},
}
... and given that the book titles are inserted into the listbox, the following code can be used to update a label named detail
with the author of the selected book:
def show_detail(event=None):
selection = listbox.curselection()
if selection:
index = selection[0]
title = listbox.get(index)
author = data[title]["author"]
detail.configure(text=f"Author: {author}")
...
listbox.bind("<<ListboxSelect>>", show_detail)
Upvotes: 1
Reputation: 3
Is this a causal python or tkinter? If it is tkinter the print() is only work in background (on the black screen) U can use Label()
def det():
for val in my_listbox:
if val==b1:
Label(text='It’s impossible to talk about mystery novels without immediately thinking of the legendary Agatha Christie. Amongst all of her works, none has a story quite as impeccably crafted as And Then There Were None, which explains why it is the best selling mystery book of all time.' ).pack()
Upvotes: 0