[Tutor] changing font

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jul 13 18:32:24 EDT 2018


On 13/07/18 18:42, Talia Koch via Tutor wrote:
> Hi, I am trying to figure out a code that would change my text output to 'Arial'

It all depends on how you are displaying your text.

If it is in a GUI such as Tkinter or WxPython you can
change the font in yor program. But the technique is
completely different in each GUI toolkit.

For example here is how to set it in Tkinter:
(Using Pyton 2.7...)

import Tkinter as tk
sans = "SansSerif 10"
serif = "Garamond 12 italic"
top = tk.Tk()
tk.Label(top, text="Hello", font=sans).pack()
tk.Label(top, text="Hello", font=serif).pack()
top.mainloop()


Note, I'm on Linux so can't use Arial, but you
should be able to change the font line to suit
your OS...


Whereas here is how to do it in wxPython:

import wx

# --- Define a custom Frame, this will become the main window ---
class FontsFrame(wx.Frame):
   def __init__(self, parent, id, title, pos, size):
        wx.Frame.__init__(self,parent, id, title, pos, size)

        # wxPython defines its own system dependant fonts
        sans = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)
        serif = wx.Font(12, wx.ROMAN, wx.ITALIC, wx.NORMAL)

        panel = wx.Panel(self)
        t1 = wx.StaticText(panel, -1, "Hello", (10,10))
        t1.SetFont(sans)
        t2 = wx.StaticText(panel, -1, "Hello", (10, 30))
        t2.SetFont(serif)

class HelloApp(wx.App):
   def OnInit(self):
       frame = FontsFrame(None, -1, "Hello", (50,50), (80,90) )
       frame.Show(True)
       self.SetTopWindow(frame)
       return True

# create instance and start the event loop
HelloApp().MainLoop()

As you see its completely different and much more work.

But if you are not using a GUI then its not something
you as a programmer can do (at least not easily).
The font the console window uses depends on your
users settings, not on your program.

You may be able to control things like underline, bold,
italic etc, but even that depends on the terminal emulator
in use.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list