pythontkintertkinter-label

python + tkinter - is there a way to have a text box with a dynamically updating text variable and a scrollbar?


I am trying to create a GUI with a text box that automatically appends status messages during the execution of a program. I can use a Label widget with a textvariable to update the text, but as far as I can tell, I can't add a scrollbar to a Label. I can add a scrollbar to a Text widget, but I can't use a textvariable to populate that widget.

I feel like there is a simple answer, but I haven't been able to find it.

This is the working Label without a scrollbar:

self.status_window = tk.Label(
    self.status_window_pane,
    height=5,
    width=50,
    textvariable=self.status_messages,
    relief=GROOVE,
    bg="white",
    fg="black",
    anchor="nw",
    justify=LEFT,
)

Solution

  • You don't need textvariable to populate a text widget, you can use its insert method:

    text = tk.Text(...)
    
    # After some event
    
    text.insert('1.0', content) # 1.0 is the text indice
    

    Some links: