pythonpython-3.xtkinterttk

Tkinter: Using ttkthemes on a Toplevel window


from tkinter import *
import tkinter.ttk as ttk
from tkinter import ttk
from ttkthemes import themed_tk as tk
import sqlite3
from PIL import Image, ImageTk

#Root window configuration
root = tk.ThemedTk()
root.get_themes()
root.set_theme('plastik')


root.geometry("500x500")
root.resizable(height=False, width=False)


def adminlogin_function():
    # adminlogin_window configuration

    # adminlogin_window = Toplevel()
    adminlogin_window =  tk.Toplevel()
    adminlogin_window.get_themes()
    adminlogin_window.set_theme('plastik')
    
    adminlogin_window.geometry("700x400")
    
    # Widgets on adminlogin_window
    admin_username_label = Label(adminlogin_window, text="Username", fg="black")
    admin_username_label.place(x=388, y=135)
    admin_username_entry = Entry(adminlogin_window, border=2)
    admin_username_entry.place(x =490 , y =135, width = 85)
    admin_pass_label = Label(adminlogin_window, text="Password", fg="black")
    admin_pass_label.place(x = 388, y= 180)
    admin_pass_entry = Entry(adminlogin_window, border=2,show="*")
    admin_pass_entry.place(x =490 , y =180, width = 85)
    admin_login_button = Button(adminlogin_window, text="Login")
    admin_login_button.place(x = 430, y= 230, width=100)
    adminlogin_window.mainloop()
adminlogin_button = ttk.Button(root, text="Admin Login", command = adminlogin_function)
adminlogin_button.place(x=250, y=160, width = 200)

How to use ttkthemes on a Toplevel() window? It is working fine for the root window but it is just not working for a top-level window. I cannot find any documentation for this


Solution

  • You don't have to manually do anything, once theme is applied to main window, all the other windows and widgets inherit the theme if you prefix it (import it from) with ttk, like:

    ttk.Label(...)
    ttk.Entry(....)
    ttk.Button(...)
    

    And also, adminlogin_window = tk.Toplevel() shoudnt have tk as your tk is actually from ttkthemes import themed_tk as tk, so just say:

    adminlogin_window =  Toplevel() #this uses toplevel from tkinter
    

    Your function would be:

    def adminlogin_function():
        # adminlogin_window configuration
        adminlogin_window =  Toplevel()    
        adminlogin_window.geometry("700x400")
        
        # Widgets on adminlogin_window
        admin_username_label = ttk.Label(adminlogin_window, text="Username")
        admin_username_label.place(x=388, y=135)
        admin_username_entry = ttk.Entry(adminlogin_window)
        admin_username_entry.place(x =490 , y =135, width = 85)
        admin_pass_label = ttk.Label(adminlogin_window, text="Password")
        admin_pass_label.place(x = 388, y= 180)
        admin_pass_entry = ttk.Entry(adminlogin_window,show="*")
        admin_pass_entry.place(x =490 , y =180, width = 85)
        admin_login_button = ttk.Button(adminlogin_window, text="Login")
        admin_login_button.place(x = 430, y= 230, width=100)
    

    Notice here I've removed fg and border option from the widget because ttk does not support those option for their widgets, it's rather tiresome to change those. I've also removed adminlogin_window.mainloop() as it's not necessary for Toplevel to have mainloop().