我正在通过 TTK 编写一个非常简单的 GUI 应用程序,我希望使按钮中文本的字体大小更大,但是当我使用 style.configure() 时它似乎不起作用。我不知道问题是什么,请帮忙。
import tkinter as tk
from tkinter import ttk
import sv_ttk
class app(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = ttk.Frame(self, relief="sunken")
container.pack(fill="both", expand = True)
self.frames = {}
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
for F in (menu, logIn, signUp):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(menu)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class menu(ttk.Frame):
def __init__(self, parent, controller):
style = ttk.Style()
style.configure('big.TButton', font=('Helvetica', 20))
ttk.Frame.__init__(self,parent,)
label = ttk.Label(self, text="Welcome to the CSV Navi!", font=("Helvetica", 40))
label.pack(expand=True, pady=100)
button = ttk.Button(self, text="Log In", style = 'big.TButton', command=lambda: controller.show_frame(logIn))
button.pack(side="left",expand=True,ipadx= 100, ipady=100)
button2 = ttk.Button(self, text="Sign Up", command=lambda: controller.show_frame(signUp),)
button2.pack(side="left", pady=100,expand=True, ipadx= 100, ipady=100)
class logIn(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self,parent)
label = tk.Label(self, text="log in")
label.pack(pady=10, padx=10)
class signUp(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self,parent)
label = tk.Label(self, text="sign up")
label.pack(pady=10, padx=10)
App = app()
sv_ttk.set_theme("dark")
App.mainloop()
这是上面的完整代码,我在菜单类中使用了样式。
class menu(ttk.Frame):
def __init__(self, parent, controller):
style = ttk.Style()
style.configure('big.TButton', font=('Helvetica', 20))
ttk.Frame.__init__(self,parent,)
label = ttk.Label(self, text="Welcome to the CSV Navi!", font=("Helvetica", 40))
label.pack(expand=True, pady=100)
button = ttk.Button(self, text="Log In", style = 'big.TButton', command=lambda: controller.show_frame(logIn))
button.pack(side="left",expand=True,ipadx= 100, ipady=100)
button2 = ttk.Button(self, text="Sign Up", command=lambda: controller.show_frame(signUp),)
button2.pack(side="left", pady=100,expand=True, ipadx= 100, ipady=100)
感谢您的帮助!
因为
sv_ttk.set_theme("dark")
是在创建app()
实例后执行的,所以app.__init__()
内部设置的主题样式被覆盖。
请在
sv_ttk.set_theme("dark")
内拨打 app.__init__()
:
class app(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
sv_ttk.set_theme("dark")
...
...
App = app()
App.mainloop()