Search code examples
python-3.xtkinterttktkinter-scrolledtext

Is there a ttk equivalent of Scrolledtext widget Tkinter


I set the theme of my main window to an awtheme 'awdark'.All the widgets with ttk extension set its appearance according to the theme by itself all except for the scrolled text widget which appears white in colour(i.e, color of the field as well as the color and look of the scrollbar)probably because its not a part of ttk.My scrolledtext widget is contained in a ttk.Frame widget by the way.Is there any workaround this?


Solution

  • Is there a ttk equivalent of Scrolledtext widget Tkinter

    No, there is not. The ttk widgets don't have a text widget.

    The scrolledtext widget is just a text widget and scrollbars, there's not much more to it. You can create your own which uses ttk scrollbars with just a few lines of code.

    Here's a solution that doesn't use classes. One that is class-based is just a couple extra lines of code.

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    frame = ttk.Frame(root)
    frame.pack(fill="both", expand=True)
    
    text = tk.Text(frame, wrap="none")
    vsb = ttk.Scrollbar(frame, command=text.yview, orient="vertical")
    hsb = ttk.Scrollbar(frame, command=text.xview, orient="horizontal")
    text.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
    
    frame.grid_rowconfigure(0, weight=1)
    frame.grid_columnconfigure(0, weight=1)
    
    vsb.grid(row=0, column=1, sticky="ns")
    hsb.grid(row=1, column=0, sticky="ew")
    text.grid(row=0, column=0, sticky="nsew")
    
    root.mainloop()