Background: with the last update my Arch Linux installation changed its standard monospace font to Inconsolata. I like this font, it works well in all my applications with one exception: in tkinter scripts all text using monospace is rendered like this:

t h i s   l o o k s   b a d .

This seems to be a rather complicated sort of bug, not located in Tcl/Tk or tkinter itself, as far as I could find out. So for the time being I'd like to change the font in tkinter scripts - but not by styling every single widget class I use. I tried two things: styling ttk.Widget and reconfiguring TkFixedfont.

In case it's important: Python version is 3.8.3.

Results: defining a style for ttk.Widget doesn't change anything:

import tkinter as tk
from tkinter import ttk
from tkinter import font

def main():
    root = tk.Tk()
    veramono = font.Font(family="Bitstream Vera Sans Mono", size=9)
    # Doesn't change anything:
    #ttk.Style().configure("TWidget", font=veramono)
    # Doesn't change anything either:
    verastyle = ttk.Style()
    verastyle.configure("TWidget", font=veramono)
    root.title("Titelschrift?")
    ttk.Label(root, text="Wie sieht das aus?").pack(
             side="top", padx=5, pady=5)
    versuchstext = tk.StringVar()
    versuchstext.set("Und das hier?")
    ttk.Entry(root, textvariable=versuchstext).pack(
              side="top", padx=5, pady=5)
    root.mainloop()

if __name__ == "__main__":
    main()

Initializing Label and Entry with "style=verastyle" results in this exception:

Traceback (most recent call last):
  File "./changefont.pyw", line 26, in <module>
    main()
  File "./changefont.pyw", line 17, in main
    ttk.Label(root, text="Wie sieht das aus?",
  File "/usr/lib/python3.8/tkinter/ttk.py", line 759, in __init__
    Widget.__init__(self, master, "ttk::label", kw)
  File "/usr/lib/python3.8/tkinter/ttk.py", line 557, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "/usr/lib/python3.8/tkinter/__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: Layout <tkinter.ttk.Style object at 0x7f3fc412a6a0> not found

And trying to reconfigure TkFixedFont like this:

def main():
    root = tk.Tk()
    fix = font.nametofont("TkFixedFont")
    fix.configure(family="Bitstream Vera Sans Mono")
    root.title("Titelschrift?")
    ttk.Label(root, text="Wie sieht das aus?", font=fix).pack(
              side="top", padx=5, pady=5)
    versuchstext = tk.StringVar()
    versuchstext.set("Und das hier?")
    ttk.Entry(root, textvariable=versuchstext).pack(
              side="top", padx=5, pady=5)
    root.mainloop()

doesn't change anything.

What's the right way to do this? Changing the standard monospace font for all of the installation would be possible, of course, but, as I said, it's quite nice for everything else.

Thank you for help,
Sibylle
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to