问题描述
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)
如何在Toplevel()窗口上使用ttkthemes?对于根窗口,它可以正常工作,但对于顶级窗口,则不起作用。我找不到与此有关的任何文档
解决方法
您无需手动执行任何操作,一旦将主题应用于主窗口,则其他所有窗口和小部件都可以在主题上加上ttk
(从中导入)来继承主题,例如:
ttk.Label(...)
ttk.Entry(....)
ttk.Button(...)
另外,adminlogin_window = tk.Toplevel()
应该有tk
,因为您的tk
实际上是from ttkthemes import themed_tk as tk
,所以只需说:
adminlogin_window = Toplevel() #this uses toplevel from tkinter
您的功能将是:
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)
这里的通知我已从窗口小部件中删除了fg
和border
选项,因为ttk
不支持其窗口小部件的选项,更改它们非常麻烦。 Ive还删除了adminlogin_window.mainloop()
,因为Toplevel
不必拥有mainloop()
希望这消除了您的疑虑,请让我知道是否有任何错误。
欢呼