Changing the background of ttk themed widgets in python - python-3.x

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?

Related

How to change Back ground colour for Tkinter module?

Hello I am very new with the python module Tkinter, I have written the code for a simple text editor. But I am not able to figure out how to change the background colour.
Help would be welcome thx.
Code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), selectbackground="grey", selectforeground="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()
The parameter you need to use is bg = "blue" for example source.
Implemented into your code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), bg="grey", fg="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()

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

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

Why do two windows appear when I connect styles?

enter image description hereI have a code like this:
from tkinter import Tk
import tkinter.ttk as ttk
"""Styles"""
style = ttk.Style()
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
foreground=[('pressed', 'white'), ('active', 'white')],
background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])
root = Tk()
button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)
root.mainloop()
I'm new at this. I searched the Internet for a solution, but could not find it. Everywhere they write about widthdraw(), but this does not help. Two windows always appear and the customized style is not applied to the button. What am I doing wrong? How do I search Google for this problem? Tell me please. Thanks.
You just need to define ttk.Style() inside the root window.
from tkinter import Tk
import tkinter.ttk as ttk
root = Tk()
"""Styles"""
style = ttk.Style()
# add the these_use option and use the 'clam' theme
style.theme_use('clam')
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
foreground=[('pressed', 'white'), ('active', 'white')],
background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])
button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)
root.mainloop()
Hope this solves the problem.

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