Azure theme 2.1 for tkinter: checkbuttons, entry and buttons

https://github.com/formazione/azure_ttk2.git

Let’s take a look at the new version of azure-theme.

Other post about the azure theme (handling errors)

Other post about azure

Other post about azure

Handling errors

Before that, however, let me show this simple code for handling errors with azure theme.

Here is an example of a tkinter app without the azure theme

Handling errors

With the azure theme style it looks like this

azure dark theme, when the input is right

And when the input is wrong…

The button is an image and is not made with the azure theme, so it’s the entry and the background due to azure

So, the dark theme is definitevely nice.

import tkinter as tk
import tkinter.ttk as ttk
from PIL import ImageTk
# create the window
root= tk.Tk()
root.title("Example Error handling")
root.geometry("400x200")

# Give it this theme (put the folder azure dark here
style = ttk.Style(root)
root.tk.call('source', 'theme/azure dark/azure dark.tcl')
style.theme_use('azure')

# Entry widget
strvar= tk.IntVar()
entry = ttk.Entry(root,textvariable=strvar)
entry.place(x=10,y=10)
def check():
    print(entry.get())
    button.config(relief="flat")
    if int(entry.get()) < 1:
        entry.state(["invalid"])

btnimage = ImageTk.PhotoImage(file=('button.png'))
button=tk.Button(root,image=btnimage,bd=0,activebackground="#333333",relief="flat", text="Click", compound=tk.CENTER, command=check)
button.place(x=5,y=50)
root.mainloop()

Version 2

Let’s go now to the new version

https://github.com/rdbende/Azure-ttk-theme

You can find the old version here

https://github.com/formazione/Azure-ttk-theme

Initialize azure theme

You can start from this for an empty window

import tkinter as tk
from tkinter import ttk



if __name__ == "__main__":
    root = tk.Tk()
    root.title("")

    # Simply set the theme
    root.tk.call("source", "azure.tcl")
    root.tk.call("set_theme", "dark")

    # Set a minsize for the window, and place it in the middle
    root.update()
    root.minsize(root.winfo_width(), root.winfo_height())
    x_cordinate = int((root.winfo_screenwidth() / 2) - (root.winfo_width() / 2))
    y_cordinate = int((root.winfo_screenheight() / 2) - (root.winfo_height() / 2))
    root.geometry("+{}+{}".format(x_cordinate, y_cordinate-20))

    root.mainloop()

You get this

the way to call the style is very simple

    # Simply set the theme
    root.tk.call("source", "azure.tcl")
    root.tk.call("set_theme", "dark")

Checkbuttons

Let’s see the code to show these checkbuttons

Checkboxes in different states
import tkinter as tk
from tkinter import ttk

class App(ttk.Frame):
    def __init__(self, parent):
        ttk.Frame.__init__(self)

        # Make the app responsive
        for index in [0, 1, 2]:
            self.columnconfigure(index=index, weight=1)
            self.rowconfigure(index=index, weight=1)

        # Create value lists
        self.option_menu_list = ["", "OptionMenu", "Option 1", "Option 2"]
        self.combo_list = ["Combobox", "Editable item 1", "Editable item 2"]
        self.readonly_combo_list = ["Readonly combobox", "Item 1", "Item 2"]

        # Create control variables
        self.var_0 = tk.BooleanVar()
        self.var_1 = tk.BooleanVar(value=True)
        self.var_2 = tk.BooleanVar()
        self.var_3 = tk.IntVar(value=2)
        self.var_4 = tk.StringVar(value=self.option_menu_list[1])
        self.var_5 = tk.DoubleVar(value=75.0)

        # Create widgets :)
        self.setup_widgets()

    def setup_widgets(self):
        self.checkboxes()

    def checkboxes(self):
        # Create a Frame for the Checkbuttons
        self.check_frame = ttk.LabelFrame(self, text="Checkbuttons", padding=(20, 10))
        self.check_frame.grid(
            row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )

        # Checkbuttons - checked, unchecked, third state, disabled
        self.check_1 = ttk.Checkbutton(self.check_frame, text="Unchecked", variable=self.var_0)
        self.check_1.grid(row=0, column=0, padx=5, pady=10, sticky="nsew")

        self.check_2 = ttk.Checkbutton(self.check_frame, text="Checked", variable=self.var_1)
        self.check_2.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")

        self.check_3 = ttk.Checkbutton(self.check_frame, text="Third state", variable=self.var_2)
        self.check_3.state(["alternate"])
        self.check_3.grid(row=2, column=0, padx=5, pady=10, sticky="nsew")

        self.check_4 = ttk.Checkbutton(self.check_frame, text="Disabled", state="disabled")
        self.check_4.state(["disabled !alternate"])
        self.check_4.grid(row=3, column=0, padx=5, pady=10, sticky="nsew")



if __name__ == "__main__":
    root = tk.Tk()
    root.title("")

    # Simply set the theme
    root.tk.call("source", "azure.tcl")
    root.tk.call("set_theme", "dark")

    app = App(root)
    app.pack(fill="both", expand=True)

    # Set a minsize for the window, and place it in the middle
    root.update()
    root.minsize(root.winfo_width(), root.winfo_height())
    x_cordinate = int((root.winfo_screenwidth() / 2) - (root.winfo_width() / 2))
    y_cordinate = int((root.winfo_screenheight() / 2) - (root.winfo_height() / 2))
    root.geometry("+{}+{}".format(x_cordinate, y_cordinate-20))

    root.mainloop()

Let’s add an entry and a button

The entry

    def setup_entry(self):
        self.entry_frame1 = ttk.LabelFrame(self, text="Entry", padding=(20, 10))
        self.entry_frame1.grid(
            row=1, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )
        self.entry = ttk.Entry(self.entry_frame1)
        self.entry.insert(0, "")
        self.entry.grid(row=0, column=0, padx=5, pady=(0, 10), sticky="ew")
        self.entry.bind("<Return>", lambda x: self.print("text:", self.entry.get()))

The button

    def setup_button(self):
        self.button_frame0 = ttk.LabelFrame(self, text="Entry", padding=(20, 10))
        self.button_frame0.grid(
            row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )
        self.accentbutton = ttk.Button(
        self.button_frame0, text="Accent button", style="Accent.TButton",
            command=lambda: self.print("Accent button: ", "pressed")
        )
        self.accentbutton.grid(row=7, column=0, padx=5, pady=10, sticky="nsew")

the whole code

import tkinter as tk
from tkinter import ttk

class App(ttk.Frame):
    def __init__(self, parent):
        ttk.Frame.__init__(self)
        self.setup_button()
        self.setup_entry()
        self.setup_checkboxes()

    def print(self, objtext, x):
        print(objtext, x)
        print()

    def setup_button(self):
        self.button_frame0 = ttk.LabelFrame(self, text="Entry", padding=(20, 10))
        self.button_frame0.grid(
            row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )
        self.accentbutton = ttk.Button(
        self.button_frame0, text="Accent button", style="Accent.TButton",
            command=lambda: self.print("Accent button: ", "pressed")
        )
        self.accentbutton.grid(row=7, column=0, padx=5, pady=10, sticky="nsew")

    def setup_entry(self):
        self.entry_frame1 = ttk.LabelFrame(self, text="Entry", padding=(20, 10))
        self.entry_frame1.grid(
            row=1, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )
        self.entry = ttk.Entry(self.entry_frame1)
        self.entry.insert(0, "")
        self.entry.grid(row=0, column=0, padx=5, pady=(0, 10), sticky="ew")
        self.entry.bind("<Return>", lambda x: self.print("text:", self.entry.get()))
    
    def setup_checkboxes(self):
        # Create control variables

        self.var_0 = tk.BooleanVar()
        self.var_1 = tk.BooleanVar(value=True)
        self.var_2 = tk.BooleanVar()
        # Create a Frame for the Checkbuttons

        self.check_frame2 = ttk.LabelFrame(self, text="Checkbuttons", padding=(20, 10))
        self.check_frame2.grid(
            row=2, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew"
        )
        # Checkbuttons - checked, unchecked, third state, disabled
        self.check_1 = ttk.Checkbutton(
            self.check_frame2, text="Unchecked", variable=self.var_0,
            command=lambda: print(self.check_1["text"] + ":", self.var_0.get()))
        self.check_1.grid(row=0, column=0, padx=5, pady=10, sticky="nsew")

        self.check_2 = ttk.Checkbutton(
            self.check_frame2, text="Checked", variable=self.var_1)
        self.check_2.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")

        self.check_3 = ttk.Checkbutton(
            self.check_frame2, text="Third state", variable=self.var_2)
        self.check_3.state(["alternate"])
        self.check_3.grid(row=2, column=0, padx=5, pady=10, sticky="nsew")

        self.check_4 = ttk.Checkbutton(
            self.check_frame2, text="Disabled", state="disabled")
        self.check_4.state(["disabled !alternate"])
        self.check_4.grid(row=3, column=0, padx=5, pady=10, sticky="nsew")



if __name__ == "__main__":
    root = tk.Tk()
    root.title("Azure dark theme")

    # Simply set the theme
    root.tk.call("source", "azure.tcl")
    root.tk.call("set_theme", "dark")

    app = App(root)
    app.pack(fill="both", expand=True)

    # Set a minsize for the window, and place it in the middle
    root.update()
    root.minsize(root.winfo_width(), root.winfo_height())
    x_cordinate = int((root.winfo_screenwidth() / 2) - (root.winfo_width() / 2))
    y_cordinate = int((root.winfo_screenheight() / 2) - (root.winfo_height() / 2))
    root.geometry("+{}+{}".format(x_cordinate, y_cordinate-20))

    root.mainloop()

Open a window from another in tkinter’s azure theme


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.