Python Forum
[Tkinter] ttk and themes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] ttk and themes (/thread-41040.html)



ttk and themes - DPaul - Nov-01-2023

Hi,
I'm trying to give my app a more modern look, so I installed :
from tkinter import *
from tkinter import ttk
from ttkthemes import ThemedTk
also I changed root = Tk() into:
root = ThemedTk(theme='plastik')
Elsewhere I find:
style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="white")
Could you pls explain the relationship between these "themes" and applying a "style".
What has one to do with the other ? Or do I have to choose?
thx,
Paul


RE: ttk and themes - DPaul - Nov-01-2023

Ok, I settled for "style".
But it is all pretty confusing, you have to find out things piecemeal.
All tk widgets exist in ttk and more..;
Oh, forgot to mention , no listbox, no canvas...
You set the style for eg. all labels... backgoundcolor = 'yellow'...
Oh, forgot to mention that for TEntry widgets, you need to use 'fieldbackgroundcolor'.
All this sets you a couple of hours back.
Keep smiling,
Paul

Edit, it does look good, once you get it working.


RE: ttk and themes - Gribouillis - Nov-01-2023

Browsing around, I came across this page. It seems that the inspection of themes and styles is a weak point of the ttk system.


RE: ttk and themes - DPaul - Nov-01-2023

(Nov-01-2023, 11:00 AM)Gribouillis Wrote: Browsing around, I came across this page
Very useful page, wish I had found it earlier.
Even here, they show 2 possibilities, but not when to use them. In my "case" it is #2.

ttk::style configure TEntry -background color
ttk::style configure TEntry -fieldbackground color

Thanks,
Paul


RE: ttk and themes - snippsat - Nov-01-2023

There are better looking libraries for Tkinter like ttkbootstrap or CustomTkinter
Quick test of ttkbootstrap,i got and error that i needed to fix locale.Error: unsupported locale setting
In dialogs.py:
#locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, ""))
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
import ttkbootstrap as ttk
from ttkbootstrap.constants import *

class DataEntryForm(ttk.Frame):
    def __init__(self, master):
        super().__init__(master, padding=(20, 10))
        self.pack(fill=BOTH, expand=YES)

        # form variables
        self.name = ttk.StringVar(value="")
        self.address = ttk.StringVar(value="")
        self.phone = ttk.StringVar(value="")

        # form header
        hdr_txt = "Please enter your contact information"
        hdr = ttk.Label(master=self, text=hdr_txt, width=50)
        hdr.pack(fill=X, pady=10)

        # form entries
        self.create_form_entry("name", self.name)
        self.create_form_entry("address", self.address)
        self.create_form_entry("phone", self.phone)
        self.create_buttonbox()

    def create_form_entry(self, label, variable):
        """Create a single form entry"""
        container = ttk.Frame(self)
        container.pack(fill=X, expand=YES, pady=5)

        lbl = ttk.Label(master=container, text=label.title(), width=10)
        lbl.pack(side=LEFT, padx=5)

        ent = ttk.Entry(master=container, textvariable=variable)
        ent.pack(side=LEFT, padx=5, fill=X, expand=YES)

    def create_buttonbox(self):
        """Create the application buttonbox"""
        container = ttk.Frame(self)
        container.pack(fill=X, expand=YES, pady=(15, 10))

        sub_btn = ttk.Button(
            master=container,
            text="Submit",
            command=self.on_submit,
            bootstyle=SUCCESS,
            width=6,
        )
        sub_btn.pack(side=RIGHT, padx=5)
        sub_btn.focus_set()

        cnl_btn = ttk.Button(
            master=container,
            text="Cancel",
            command=self.on_cancel,
            bootstyle=DANGER,
            width=6,
        )
        cnl_btn.pack(side=RIGHT, padx=5)

    def on_submit(self):
        """Print the contents to console and return the values."""
        print("Name:", self.name.get())
        print("Address:", self.address.get())
        print("Phone:", self.phone.get())
        return self.name.get(), self.address.get(), self.phone.get()

    def on_cancel(self):
        """Cancel and close the application."""
        self.quit()

if __name__ == "__main__":
    app = ttk.Window("Data Entry", "superhero", resizable=(False, False))
    DataEntryForm(app)
    app.mainloop()
[Image: mqnCfg.png]


RE: ttk and themes - DPaul - Nov-01-2023

(Nov-01-2023, 03:53 PM)snippsat Wrote: There are better looking libraries for Tkinter
Hi, indeed, looking very appealing! These alternatives do not get much publicity though.
I'm happy to get the ttk working for 95% after many hours.
Having a "disabledbackground" color for TEntry however, remains elusive. Even with a list.
A bit of a mess, if you ask me.
Anyone know if a new version of tKinter is in the pipeline?
Paul


RE: ttk and themes - snippsat - Nov-01-2023

(Nov-01-2023, 06:21 PM)DPaul Wrote: Hi, indeed, looking very appealing! These alternatives do not get much publicity though.
Hmm i would say that this is not the case CustomTkinter has 8,7k stars,and ttkbootstrap 1,3k stars and ttkthemes 325.
I would definitely choose them for the better look,if i should make a GUI with Tkinter.
Older Tkinter has never been a option for me,because of the old GUI look(now with new libraries has that changed).
I has used wxPython a lot before,and a little now called Qt for Python.
Now in later years more web app stuff(can be GUI looking) like eg Flask.
Quote:Anyone know if a new version of tKinter is in the pipeline?
Python follow releases at Tlc/Tk ,so Python 3.12 has version 8.6.


RE: ttk and themes - DPaul - Nov-02-2023

(Nov-01-2023, 07:51 PM)snippsat Wrote: I would definitely choose them for the better look,if i should make a GUI with Tkinter.
OK, OK, you made your point. Wink
I am tempted by this bootstrap thing and one or 2 of the themes on offer.
The reason being, I'm not a color stylist, and I presume the theme authors are.
So I can offer a more coherent Gui form a style point of view.
What is stopping me ?
After many hours, I got the (simple) TTK working for 95%.
If only I could find that the 5% I'm missing will be available in said bootstrap.
(disabled background color)
Also it seems that the padding is done on the widget level, while it is
now on the style level (less typing)
I'll try to find some better examples.
Thx,
Paul


RE: ttk and themes - DPaul - Nov-02-2023

Epilog.
Although not standard in ttk I managed to get disabled-background-color
working for my 4 TEntrys. (simply make a different style number for each one).
I have looked into "bootstrap", which seems very attractive, but the search
box in their documentation gives no hits on "background-color-disabled" in any combination.
I will attempt "bootstrap" when convinced that this feature is readily available.
I spent enough hours on making the simple one work, and Montesquieu is often right.
Paul


RE: ttk and themes - rob101 - Dec-02-2023

I find that a GUI design app does much of the heavy lifting and although I don't use such for relatively simple designs, I do find that with more complex GUIs a framework such as PAGE is a god send.

Right now, the GUI "themes" that PAGE offers, are somewhat limited, but I know that this is going to be improved, moving forward and the technical support that one gets is second to none.