From nulla.epistola at web.de Tue Jul 21 11:34:55 2020 From: nulla.epistola at web.de (Sibylle Koczian) Date: Tue, 21 Jul 2020 17:34:55 +0200 Subject: [Tkinter-discuss] Force tkinter to use another fixed font Message-ID: 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 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 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 From paul at tartan.co.za Tue Jul 21 12:30:38 2020 From: paul at tartan.co.za (Paul Malherbe) Date: Tue, 21 Jul 2020 18:30:38 +0200 Subject: [Tkinter-discuss] Force tkinter to use another fixed font In-Reply-To: References: Message-ID: <7c74a7c9-7065-103b-9330-372699d2fc14@tartan.co.za> An HTML attachment was scrubbed... URL: From nulla.epistola at web.de Wed Jul 22 12:36:16 2020 From: nulla.epistola at web.de (Sibylle Koczian) Date: Wed, 22 Jul 2020 18:36:16 +0200 Subject: [Tkinter-discuss] Force tkinter to use another fixed font In-Reply-To: <7c74a7c9-7065-103b-9330-372699d2fc14@tartan.co.za> References: <7c74a7c9-7065-103b-9330-372699d2fc14@tartan.co.za> Message-ID: <0112a5ea-d64e-27b4-a2f3-84acf629129b@web.de> Am 21.07.2020 um 18:30 schrieb Paul Malherbe: > Hi > > ttk.Entry and ttk.Label use TkDefaultFont therefore changing TkFixedFont > will not work. > Thank you for making me look at this - it's even worse: ttk.Label uses TkDefaultFont, ttk.Entry uses TkTextFont. So that's definitely not the right way. > use > > style.configure("TLabel", font=yourfont) > > style.configure("TEntry", font=yourfont) > That's exactly the method I'd like to avoid: even a small to middling application will use more than two different widget classes, and every single one of them would need a separate call to style.configure. Every change of the chosen font would have to be repeated for every widget class. I had hoped that a style defined for the base class would be inherited by subclasses, but that doesn't seem to be the case. Will try next with setting a font for the Tk() instance. Regards, Sibylle From nulla.epistola at web.de Tue Jul 28 04:23:59 2020 From: nulla.epistola at web.de (Sibylle Koczian) Date: Tue, 28 Jul 2020 10:23:59 +0200 Subject: [Tkinter-discuss] Force tkinter to use another fixed font In-Reply-To: <0112a5ea-d64e-27b4-a2f3-84acf629129b@web.de> References: <7c74a7c9-7065-103b-9330-372699d2fc14@tartan.co.za> <0112a5ea-d64e-27b4-a2f3-84acf629129b@web.de> Message-ID: <8ac3624b-f74b-056e-1394-4b7efa3004d4@web.de> Am 22.07.2020 um 18:36 schrieb Sibylle Koczian: > Am 21.07.2020 um 18:30 schrieb Paul Malherbe: >> Hi >> >> ttk.Entry and ttk.Label use TkDefaultFont therefore changing TkFixedFont >> will not work. >> > Thank you for making me look at this - it's even worse: ttk.Label uses > TkDefaultFont, ttk.Entry uses TkTextFont. So that's definitely not the > right way. > No, I think now it _is_ a comparatively simple way: import tkinter as tk from tkinter import ttk from tkinter import font def changefonts(): for fontname in [f for f in font.names() if font.nametofont(f).actual()["family"] == "Inconsolata"]: font.nametofont(fontname).config( family="Bitstream Vera Sans Mono") def main(): root = tk.Tk() changefonts() ###... build your gui root.mainloop() if __name__ == "__main__": main() This should work for all themes. It seems unnecessary on Windows and on Linux problems with the fonts probably depend on the chosen desktop manager. Regards Sibylle