Open new windows once from another one with azure tkinter theme

We are still taking care of the azure theme for tkinter that is gone to a new version 2.0. I made also a new fork that you can find here. In the open_new_window2.py script you can see how I made possible to open a new window from the another. I made this yet for the default theme for tkinter.

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

I adapted to the azure dark them the code to open a new window from another in tkinter

import tkinter as tk
from tkinter import ttk


class App(ttk.Frame):
    ''' the root window '''
    def __init__(self, parent):
        ttk.Frame.__init__(self)
        self.win2_status = 0 # to open the new window once
        self.setup_frames()
        self.setup_button()

    def setup_frames(self):
        # frame for button --------------- frame 0 / button
        self.button_frame0 = ttk.LabelFrame(self, text="Azure dark Theme App", padding=(20, 10))
        self.button_frame0.grid(
            row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew")

    def setup_button(self):
        self.accentbutton = ttk.Button(
        self.button_frame0,
        text="CLick to open new window", 
        style="Accent.TButton",
        command=lambda: self.new_window(Win2)
        )
        self.accentbutton.grid(row=7, column=0, padx=5, pady=10, sticky="nsew")


    def new_window(self,winclass):
        if self.win2_status == 0:
            try:
                if self.win2.status == 'normal': # if it's not created yet
                    self.win2.focus_force()
            except:
                    self.win2 = Win2(root) # populate
                    self.win2_status = 1
                    # self.win2 = tk.Toplevel(root) # create

class Win2(ttk.Frame):
    ''' the 2nd window '''
    def __init__(self, root):
        ttk.Frame.__init__(self)
        # self.root = tk.Toplevel(root)
        self.root = tk.Tk()
        self.root.title("Azure dark theme")
        # Simply set the theme
        self.root.tk.call("source", "azure.tcl")
        self.root.tk.call("set_theme", "dark")
        self.root.geometry("300x300+500+200")
        self.root.protocol("WM_DELETE_WINDOW", self.close)
        self.setup_button()


    def setup_button(self):
        self.accentbutton = ttk.Button(
        self.root,
        text="CLick to open new window", 
        style="Accent.TButton",
        command=lambda: print("Hello")
        )
        self.accentbutton.grid(row=7, column=0, padx=5, pady=10, sticky="nsew")


    def close(self):
        print("Window destroyed")
        app.win2_status = 0
        self.root.destroy()


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()
How to open a new window from another with the azure theme in tkinter

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.