Meniya
Meniya

Reputation: 197

ttk Entry widget ignores font applied via style?

from tkinter import Tk
from tkinter.ttk import Style, Entry
import tkinter.font as tkfont

root = Tk()

font = tkfont.Font(family='Helvetica', size=30, slant='italic')
style = Style()
style.configure('Custom.TEntry', font=font, foreground='green')
entry_font = Entry(root, font=font, foreground='green')
entry_font.insert(0, 'directly configured')
entry_font.pack()
entry_style = Entry(root, style='Custom.TEntry')
entry_style.insert(0, 'styled entry')
entry_style.pack()

root.mainloop()

The first entry responds to the font whereas the second does not. Is there a way to apply the font using styles?

Upvotes: 2

Views: 876

Answers (4)

toyota Supra
toyota Supra

Reputation: 4595

The first entry responds to the font whereas the second does not. Is there a way to apply the font using styles?

The problem can be fixed.

  • Add ttk. prefix for Entry widgets.
  • Add style='Custom.TEntry' for Entry widgets parameter.

Snippet:

from tkinter import Tk
from tkinter.ttk import Style, Entry
import tkinter.font as tkfont


root = tk.Tk()

font = tkfont.Font(family='Helvetica', size=30, slant='italic')
style = ttk.Style()
style.configure('Custom.TEntry', foreground='green') 

entry_font = ttk.Entry(root,
                       style='Custom.TEntry',
                       font=font)
entry_font.insert(0, 'directly configured' )
 
entry_style = ttk.Entry(root,
                        style='Custom.TEntry',
                        font=font)

entry_style.insert(0, 'styled entry')

entry_font.pack()
entry_style.pack()

root.mainloop()

Screenshot:

enter image description here

Upvotes: -1

j123b567
j123b567

Reputation: 3439

There are some workarounds.

You can add semi-direct font=style.lookup("Custom.TEntry", "font") to your Entry. It is not pretty but you can still have styles in one place.

from tkinter.ttk import Style, Entry

style = Style()
style.configure('Custom.TEntry', font=('sans-serif', 30), foreground='green')

# somewhere else in the code
style = Style()
entry = Entry(root, style="Custom.TEntry", font=style.lookup("Custom.TEntry", "font"))

You can also subclass original Entry and fix it.

from tkinter.ttk import Style, Entry

class StyledEntry(Entry):
    def __init__(self, master=None, widget=None, **kw):
        if "font" not in kw:
            style = kw.get("style", "TEntry")
            kw["font"] = Style().lookup(style, "font")

        super().__init__(master=master, widget=widget, **kw)

# and later just
entry = StyledEntry(root, style="Custom.TEntry")

Upvotes: 0

Meniya
Meniya

Reputation: 197

from: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Entry.html

Use this option to specify the font of the text that will appear in the widget; see Section 5.4, “Type fonts”. For reasons that are unclear to the author, this option cannot be specified with a style.

guess i'll do it directly

Upvotes: 3

Ron Norris
Ron Norris

Reputation: 2690

Yes, but you can configure the font in the tk manner:

entry_font.configure(font=('TkTextFont', 20))

Upvotes: -1

Related Questions