1

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?

Iron man
  • 13
  • 5

1 Answers1

2

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you sir for this code,it helped me but can I also change the color of the cursor as well tow white – Iron man Nov 10 '20 at 19:32
  • @Ironman: Yes, the color of the cursor is configurable with the `insertbackground` option. – Bryan Oakley Nov 10 '20 at 19:39
  • Thanks once again – Iron man Nov 10 '20 at 20:22
  • @BryanOakley This is nice, but when I want to make a class that behaves like ScrolledText (by just inheriting from tkinter.Text) I can't just grid or pack things in the constructor. Can you help with code for this case? – petro4213 Feb 21 '22 at 10:28
  • @petro4213: don't inherit from a `Text` widget. Instead, inherit from a `Frame`. That will allow you to place both scrollbars and the text widget together. – Bryan Oakley Feb 21 '22 at 15:50
  • @BryanOakley But then I have to duplicate all methods the Text widget has and redirect them to the Text widget, which then is kind of a sub-widget, but not the base class anymore. In Perl/Tk there's the concept of a "MegaWidget", which allows for constructing compound widgets from basic widgets packed or gridded into some layout. There you can specify which methods are delegated to which sub-widget. I couldn't find anything similar in tkinter and Pwm looks more like a coterie to me. – petro4213 Feb 22 '22 at 12:05
  • @petro4213: "duplicating the methods" takes probably less than a dozen lines of code. – Bryan Oakley Feb 22 '22 at 15:08
  • @BryanOakley This maybe right for a specific widget, but it is not very elegant and certainly not what object oriented programming is all about... I think I'll just use a Text widget as base class and add the scrollbars externally - also ugly, but not that much... – petro4213 Feb 24 '22 at 12:11
  • @petro4213: this has nothing to do with elegance or object oriented programming. You can inherit from text if you want, but putting scrollbars inside the text widget will overlap some of the text. Combining three objects inside a fourth is called composition, and it's a common object-oriented pattern. – Bryan Oakley Feb 24 '22 at 15:19