Search code examples
tkintertkinter-entryttkpython-3.10python-3.11

ttk Entry widget font being overwritten by theme


Does anyone know why the ttk.Entry widget font is being overwritten when creating the widget outside of a function called by a Button but it retains the font correctly if you create the widget using a ttk.Button?

import tkinter as tk
from tkinter import ttk
import sv_ttk

def newEntry():
    entry = ttk.Entry(gui, font="Calibri 14 bold")
    entry.pack()

gui = tk.Tk()

sv_ttk.set_theme("dark")

entry = ttk.Entry(gui, font="Calibri 14 bold")
entry.pack()

button = ttk.Button(gui, text="New Entry", command=lambda: newEntry())
button.pack()

gui.mainloop()

I have tried using ttk.Styles() to add a style to the ttk.Entry widget and that does not work either.


Solution

  • You should update your gui after setting the theme to dark:

    import tkinter as tk
    from tkinter import ttk
    import sv_ttk
    
    def newEntry():
        entry = ttk.Entry(gui, font="Calibri 14 bold")
        entry.pack()
    
    gui = tk.Tk()
    
    sv_ttk.set_theme("dark")
    gui.update()
    
    entry = ttk.Entry(gui, font="Calibri 14 bold")
    entry.pack()
    
    button = ttk.Button(gui, text="New Entry", command=lambda: newEntry())
    button.pack()
    
    gui.mainloop()