如何在 tkinter 中更改菜单字体大小?

问题描述 投票:0回答:2

我想要更大尺寸的菜单项。它适用于菜单命令,但大小也有限。例如,如果我设置 font = 200,它与 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

python tkinter widget treeview ttk
2个回答
2
投票

如果您想设置字体 size 而不使用自定义字体 face,您可以使用

font
参数的元组,如下所示:
("", 50)
。这是一个完整的工作示例,用于实现您的类,设置所有菜单标签的字体大小:

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()

""
中的
("", 50)
为字体;这里是空的,因为不需要设置字体。如果您想使用自定义面孔,例如
Mono
,只需将
""
替换为
"Mono"


0
投票

您还可以通过添加以下行来调整菜单字体和大小:

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

这里将其应用于您的示例:

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()

如果您以后想更改它,这将使管理更容易。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.