Search code examples
pythontkinterttk

TTK style.configure for button font size does not work


I am writing a very simple GUI application through TTK where I wish to make the font size of the text in the button bigger, however it seems to not work when I use style.configure(). I have no idea what the problem is please help.

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()

That is the full code above and I have used style in the menu class.

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)

Appreciate your help!


Solution

  • It is because sv_ttk.set_theme("dark") is executed after creating the instance of app(), so the theme style set inside app.__init__() is override.

    Call sv_ttk.set_theme("dark") inside app.__init__() instead:

    class app(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
            sv_ttk.set_theme("dark")
            ...
    
    ...
    
    App = app()
    App.mainloop()