0

Hi all,
im a beginner to programming Python with Tkinter.
I have a windows with 2 form: 1 Listbox and 1 Textarea that load a txt file.
The Listbox contains a name of chapters.
How can I make because click on the listbox scroll the text inside the textarea a the corresponding chapter?
Thanks to all
Hud

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by hudson5cp
0

Here is an example that should get most of the way there. All you need to do is to supply the search string from the listbox selection:

# searching a long text for a string and scrolling to it
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to paste, and ctrl+/ to select all

import Tkinter as tk

def display(data):
    """creates a text display area with a vertical scrollbar"""
    scrollbar = tk.Scrollbar(root)
    text1 = tk.Text(root, yscrollcommand=scrollbar.set)
    text1.insert(0.0, data)
    scrollbar.config(command=text1.yview)
    scrollbar.pack(side='right', fill='y')
    text1.pack(side='left', expand=0, fill='both')
    
    # could bring the search string in from a listbox selection
    pattern = "Chapter 3"
    # line.char(lines start at 1, characters at 0)
    start = "1.0"
    # returns eg. "31.0" --> line 31, character 0
    index = text1.search(pattern, start)
    # scroll text until index line is visible
    # might move to the top line of text field
    text1.see(index)
    
    
root = tk.Tk()

str1 = """\
Chapter 1
.
.
.
.
.
.
.
.
.
.
.
.
.
Chapter 2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Chapter 3
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The End
"""

display(str1)

root.mainloop()
0

Ok thanks for the help. This problem is solved!
Hours trying to call through the listbox...
Bye Hud

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.