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

Recommended Answers

All 2 Replies

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()

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.