Mihai
Mihai

Reputation: 87

How to change menu font size in tkinter?

I want a bigger size for menu's items. It works for menu's commands but also limited size. For example if I set font = 200 it's no difference than font = 50.

class Window(Frame):
    #define constructor
    def __init__(self, master):
    
        #initialize class's attributes
        Frame.__init__(self, master)
        self.master = master
        
    def login(self):
        pass
    
    def menu_function(self):
        #create main menu
        menu = Menu(self.master)
        self.master.config(menu = menu)
        booking       = Menu(menu)
        file          = Menu(menu)
        statistics    = Menu(menu)
        file.add_command(label = "Exit", font = 50)
        statistics.add_command(label = "Age", font = 50)
        statistics.add_command(label = "Gender", font = 50)
        statistics.add_command(label = "Interests", font = 50)
        menu.add_cascade(label = "Booking", menu = booking)
        menu.add_cascade(label = "File", menu = file)
        menu.add_cascade(label = "Statistics", menu = statistics)

enter image description here

Upvotes: 0

Views: 3939

Answers (2)

Inyoka
Inyoka

Reputation: 1446

On Windows (only) the size of the menu headings (File, Edit Help etc) is set by Windows and cannot be changed. The actual menu text can (see figures at end of answer).

You can adjust the menu font and size by adding the line :

root.option_add("*Menu.Font", "Helvetica 16")

Here it is applied to a simplified version of your example :

from tkinter import *

class Window(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        
    def menu_function(self):
        menu = Menu(self.master)
        self.master.config(menu=menu)
    
        file = Menu(menu, tearoff=0)
        file.add_command(label="Exit")
        menu.add_cascade(label="File", menu=file)
    
        statistics = Menu(menu, tearoff=0)
        statistics.add_command(label = "Age")
        statistics.add_command(label = "Gender")
        statistics.add_command(label = "Interests")
        menu.add_cascade(label="Statistics", menu=statistics)

root = Tk()
root.geometry("800x400")
root.option_add("*Menu.Font", "Helvetica 25") # Change menu font and size.
frame = Window(root)
frame.pack()
frame.menu_function()
root.mainloop()

If you like you can save the original font like this :

import tkinter.font as tkfont
original_font = tkfont.nametofont("TkMenuFont")

... and reapply the standard font ...

root.option_add("*Menu.Font", f"{original_font} 25") # Change menu font and size.

... or just adjust the size of the font in place like this :

original_font.configure(size=25)

Linux (wsl) example with the Terminal font applied :

Example shown using the Terminal font

Windows example, the best you can achieve without using buttons in place of menus (to the best of my knowledge) :

enter image description here

Upvotes: 0

If you want to set the font size without using a custom font face, you can use a tuple for the font argument, like this: ("", 50). Here's a complete working example implementing your class, setting the font size for all menu labels:

from tkinter import *

class Window(Frame):
    #define constructor
    def __init__(self, master):
    
        #initialize class's attributes
        Frame.__init__(self, master)
        self.master = master
        
    def login(self):
        pass
    
    def menu_function(self):
        #create main menu
        menu = Menu(self.master)
        self.master.config(menu = menu)
        booking       = Menu(menu)
        file          = Menu(menu)
        statistics    = Menu(menu)
        file.add_command(label = "Exit", font = ("", 50))
        statistics.add_command(label = "Age", font = ("", 50))
        statistics.add_command(label = "Gender", font = ("", 50))
        statistics.add_command(label = "Interests", font = ("", 50))
        menu.add_cascade(label = "Booking", menu = booking, font = ("", 50))
        menu.add_cascade(label = "File", menu = file, font = ("", 50))
        menu.add_cascade(label = "Statistics", menu = statistics, font = ("", 50))

root = Tk()
frame = Window(root)
frame.pack()
frame.menu_function()
root.mainloop()

The "" in ("", 50) is the font face; here, it's empty, because there's no need to set the font face. If you want to use a custom face, for example Mono, just replace "" with "Mono".

Upvotes: 2

Related Questions