Is there a ttk equivalent of Scrolledtext widget Tkinter - python-3.x

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?

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

Related

Why my ttk style won't apply to my Frame?

I've been fighting with ttk style for about 2 days right now, but nothing seems to work. I've tried a lot of different approaches, but I had no effect. Right now, I just want to see that the Frame that I'm trying to add to my App is working, could you please show me where is the problem?
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
# Main window configuration
self.title("Work Tracker")
self.geometry("600x700")
self.resizable(0,0)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Styles
style = ttk.Style()
style.configure('TFrame', background='blue')
# Logo frame
frame = ttk.Frame(self, width=200, height=150)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_propagate(False)
frame.grid(row=0, column=0, sticky='nw')
label = ttk.Label(frame, text ="WORK TRACKER")
label.pack()
def main():
app = App()
app.mainloop()
main()

Changing the background of ttk themed widgets in python

I am currently trying to learn ttk themed widgets. I wanted to change the background colour of my ttk button. I followed to ttk docs and written this:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("GUI App One")
root.geometry("800x500")
root.maxsize(800,500)
root.minsize(800,500)
Style().configure("C.TButton", padding=6, background="blue", relief="raised")
Style().configure("B.TButton", font=("Arial",30))
Style().configure("Elem.TFrame", background="red")
backframe = Frame(root, width=800, height=500, style="Elem.TFrame")
backframe.place(x=0, y=0)
print()
btn1 = Button(backframe, text="Click me", style="C.TButton")
btn1.place(x=20, y=50)
btn2 = Button(backframe, text="Click me too", style="B.TButton")
btn2.place(x=100, y=100)
mainloop()
in 'C.TButton' styling I tried to change the background colour of the 'btn1', but it only changes border colour to blue and not the background colour. How can I change the background colour?

Tkinter style not being applied

I'm simply trying to change to "clam" style but nothing changes. I have a more expansive UI written with other widgets, so I know that nothing changes when I try to change the style:
I'm working in Linux Mint 19.2, Python 3.6.8, and using Pycharm. I've verified that Pycharm is not the issue by running from terminal.
from tkinter import *
from tkinter.ttk import Style
class Window(Frame):
def __init__(self, master=None):
self.master = master
self.add_widgets()
def add_widgets(self, ):
self.mainframe = Frame(self.master, border=3)
self.mainframe.grid(columnspan=3, sticky='w', padx=5, pady=5)
Label(self.mainframe, text="Directory Selection", font='Helvetica 12 bold italic underline').grid(column=0, sticky="w")
self.pathframe = Frame(self.mainframe, relief=RIDGE, border=2)
self.pathframe.grid(rowspan=3, columnspan=3, sticky='w', padx=0, pady=10)
button_color="dark gray";
Button(self.pathframe, text="Run Folder Path", bg=button_color).grid(sticky="w")
Entry(self.pathframe, width=43).grid(row=0, column=1, sticky="w")
root = Tk()
style = Style()
style.theme_use("clam")
root.title('TestUI')
root.geometry("500x500")
app = Window(root)
root.mainloop()
I also tried:
style = Style(root)
style.theme_use("clam")
and still no change.
You're not using any ttk widgets. The normal tkinter widgets aren't affected by the ttk styles.
Typically this is done by importing ttk and then prefixing the widgets with ttk
from tkinter import ttk
...
self.mainframe = ttk.Frame(self.master, border=3)
...

how to set a theme for ttkthemes for python3 in windows10?

I'm trying to style my tkinter GUI with some theme from ttkthemes.
I have found this code :
from ttkthemes import ThemedStyle
import tkinter as tk
from tkinter import ttk
app = tk.Tk()
app.title('App')
style = ThemedStyle(app)
style.set_theme("black")
tktext = tk.Label(app, text=" tk Label")
tktext.pack()
tkbutton = tk.Button(app, text="tk Button")
tkbutton.pack()
text = ttk.Label(app, text=" ttk Label")
text.pack()
button = ttk.Button(app, text="ttk Button")
button.pack()
app.geometry('200x200')
app.mainloop()
in this topic :
Python - How do I add a theme from ttkthemes package to a guizero application?
but I have a problem with that and that's when I run the program the theme doesn't cover whole root windows and just the buttons or labels from ttk are taking the theme (using windows10).I have tried in some other codes and everytime the same problem.
What's Problem with that?
ttktheme
Themes only apply to widgets from the ttk module. For widgets not in ttk, such as the text widget, you have to configure them individually.

Can't change button font size in tkinter

I can't seem to change the size of my font in tkinter! No matter which size I choose, the button text displays the same. If I deleted the whole stlye line, it's displayed smaller.
Similarly, the font always looks the same, no matter what I choose.
I want to finetune the size and the font, can you please help me=?
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)
ttk.Style().configure("TButton", font=font.Font(family='wasy10', size=80)) #I can choose any value here instead of "80" and any font like "Helvetica" - nothing will change
root.mainloop()
You do not need to import font. ttk style has its own font argument.
Just put the style in the first option and the font size in the 2nd option.
I would also use the variable name to edit the style. Instead of calling:
ttk.Style().configure()
Do this:
style.configure()
Take a look at the below.
import tkinter
import tkinter.ttk as ttk
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
style.configure("TButton", font=('wasy10', 80))
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()
On the advice of Bryan Oakley in the comments here is a 2nd option that is close to what you are trying to do with fort.
This option saves a referent to the font object and then uses it to update the style.
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
font = font.Font(family="wasy10", size=80)
style.configure("TButton", font=font)
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()

Resources