Re: Force tkinter to use another fixed font

Paul Malherbe <[email protected]> Tue, 21 Jul 2020 18:30:38 +0200
Newsgroups gmane.comp.python.tkinter
Organization Tartan Financial Systems
Message-ID <[email protected]>
--===============3467727359986613639==
Content-Type: text/html; charset=utf-8
Content-Language: en-GB
Content-Transfer-Encoding: 7bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi</p>
    <p>ttk.Entry and ttk.Label use TkDefaultFont therefore changing
      TkFixedFont will not work.</p>
    <p>use</p>
    <p>style.configure("TLabel", font=yourfont)</p>
    <p>style.configure("TEntry", font=yourfont)<br>
    </p>
    <div class="moz-signature">
      <meta content="text/html; charset=UTF-8" http-equiv="content-type">
      <title>signature</title>
      Regards<br style="font-weight: bold;">
      <br style="font-weight: bold;">
      Paul Malherbe<br>
      <br>
      <br>
    </div>
    <div class="moz-cite-prefix">On 21/07/2020 17:34, Sibylle Koczian
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:[email protected]">
      <pre class="moz-quote-pre" wrap="">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 &lt;module&gt;
     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 &lt;tkinter.ttk.Style object at 0x7f3fc412a6a0&gt; 
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
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/tkinter-discuss">https://mail.python.org/mailman/listinfo/tkinter-discuss</a>
</pre>
    </blockquote>
  </body>
</html>

--===============3467727359986613639==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss

--===============3467727359986613639==--