[Python] Where do i place/create a dynamically created widget that controls elided text in...

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    i am building a python tkinter editor / IDE and want to create a widget that can be clicked on to collapse / expand elided text.

    i am not sure what widget is best to use (button, label, image).

    think of a method similar to visual studio code. they have a widget to the left of the text box line that is clickable. i want something similar using python tkinter.

    i currently have 2 text boxes side by side. the narrow left one is for the line number and the larger right one is for the code text that you can edit.

    when i type a keyword like 'Function' i want a widget to be dynamically created that will control elided text so i can collapse / expand the text inside this function.

    i already know how to set tags for the area to be elided and know how to check for key words. i just don't know where to put the widget or what type it should be.

    here is a small example of trying to use buttons as control:

    import tkinter as tk

    root = tk.Tk()
    root.title("Testing widgets for Elide")

    # create 'line number' text box
    line_text = tk.Text(root, wrap="none", width=5, insertwidth=0) # don't want the cursor to appear here
    line_text.pack(fill="y", side="left")

    # create 'code' text box
    text_box = tk.Text(root, wrap="none")
    text_box.pack(fill="both", expand=True, side="left")

    # add a tag to line number text box (need text to be at the right side)
    line_text.tag_configure("right", justify="right")

    # add some text into the text boxes
    for i in range(13):
    line_text.insert("end", "%s \n" % (i+1))
    line_text.tag_add("right", "1.0", "end")

    for i in range(13):
    text_box.insert("end", "%s \n" % ("some text here at line number #" + str(i+1)))

    # add button to use as elide +/- (inside text boxes? _ not sure which widget is correct (button, label, image)? _ also can't work out spacing so button doesn't interfere with text)
    elide_button = tk.Button(text_box, text="-")
    text_box.window_create("11.0", window=elide_button) # *** test ***

    elide_button2 = tk.Button(line_text, text="-")
    line_text.window_create("11.0" + "lineend-1c", window=elide_button2) # *** test ***


    screen shot of testing buttons

    i have tried using a button inside the main text box with a "-" character but it was too big and pushed the main text line over to fit in. i don't want this.

    i tried using a button in the narrow line number text box (at the line end) but it also pushed the text over the other way. i don't want this.

    basically i don't want the text interfered with by the button. is there some kind of black magic or spacing i need to know about, lol?

    i am still newish to python & newish to tkinter. i tried searching for this but couldn't find what i wanted.

    thanks for any help, steve.

    Continue reading...

Compartilhe esta Página