📜  如何更改 Tkinter 标签字体大小?

📅  最后修改于: 2022-05-13 01:55:32.796000             🧑  作者: Mango

如何更改 Tkinter 标签字体大小?

Tkinter Label 用于显示一行或多行,也可用于显示位图或图像。在本文中,我们将更改 Label Widget 的字体大小。要创建标签使用以下内容:

我们可以使用不同的方法来做到这一点:

方法一:通过使用Label的字体属性。

Python3
# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.ttk import Label
  
  
# Creating App class which will contain
# Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Creating second label
        # This label has a font-family of Arial
        # and font-size of 25
        Label(self.master,
              text="I have a font-size of 25",
  
              # Changing font-size here
              font=("Arial", 25)
              ).pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()


Python3
# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.ttk import Label, Style
  
  
# Creating App class which will contain
# Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Instantiating Style class
        self.style = Style(self.master)
  
        # Configuring Custom Style
        # Name of the Style is "My.TLabel"
        self.style.configure("My.TLabel", font=('Arial', 25))
  
        # Creating second label
        # This label has a font-family of Arial
        # and font-size of 25
        Label(
            self.master,
            text="I have a font-size of 25",
  
            # Changing font-size using custom style
            style="My.TLabel").pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()


Python3
# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.ttk import Label, Style
  
  
# Creating App class which will contain
# Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Instantiating Style class
        self.style = Style(self.master)
  
        # Changing font-size of all the Label Widget
        self.style.configure("TLabel", font=('Arial', 25))
  
        # Creating second label
        # This label has a font-family of Arial
        # and font-size of 25
        Label(self.master, text="I have a font-size of 25").pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()


Python3
# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.font import BOLD, Font
from tkinter.ttk import Label
  
  
# Creating App class which will contain Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Creating Font, with a "size of 25" and weight of BOLD
        self.bold25 = Font(self.master, size=25, weight=BOLD)
  
        # Creating second label
        # This label has a default font-family
        # and font-size of 25
        Label(self.master, text="I have a font-size of 25",
              font=self.bold25).pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()


输出:

方法 2:通过使用 Style 类。在这种方法中,我们将使用我们的自定义样式,否则所有 Label 小部件都将获得相同的样式。

蟒蛇3

# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.ttk import Label, Style
  
  
# Creating App class which will contain
# Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Instantiating Style class
        self.style = Style(self.master)
  
        # Configuring Custom Style
        # Name of the Style is "My.TLabel"
        self.style.configure("My.TLabel", font=('Arial', 25))
  
        # Creating second label
        # This label has a font-family of Arial
        # and font-size of 25
        Label(
            self.master,
            text="I have a font-size of 25",
  
            # Changing font-size using custom style
            style="My.TLabel").pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()

输出:

注意:上述方法中, TLabel是默认样式的名称。因此,如果您想创建自己的样式名称,请始终使用以下语法

如果您只使用默认样式名称,那么它将适用于所有相应的小部件,即如果我使用TLabel而不是My.TLabel ,那么两个标签的字体大小都将为 25。重要的是,如果您使用默认样式名称,那么您不需要提供 style 属性。

额外:使用默认样式名称更改字体大小。

蟒蛇3

# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.ttk import Label, Style
  
  
# Creating App class which will contain
# Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Instantiating Style class
        self.style = Style(self.master)
  
        # Changing font-size of all the Label Widget
        self.style.configure("TLabel", font=('Arial', 25))
  
        # Creating second label
        # This label has a font-family of Arial
        # and font-size of 25
        Label(self.master, text="I have a font-size of 25").pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()

请注意,在上面的程序中,我们没有为任何 Label 提供样式或字体属性,但它们仍然具有相同的字体大小和相同的字体系列。

输出:

方法 3:通过使用Font类。在这个方法中,我们将创建一个 Font 对象,然后使用它来更改任何小部件的字体样式。受益于此

蟒蛇3

# importing tkinter module and Widgets
from tkinter import Tk
from tkinter.font import BOLD, Font
from tkinter.ttk import Label
  
  
# Creating App class which will contain Label Widgets
class App:
    def __init__(self, master) -> None:
  
        # Instantiating master i.e toplevel Widget
        self.master = master
  
        # Creating first Label i.e with default font-size
        Label(self.master, text="I have default font-size").pack(pady=20)
  
        # Creating Font, with a "size of 25" and weight of BOLD
        self.bold25 = Font(self.master, size=25, weight=BOLD)
  
        # Creating second label
        # This label has a default font-family
        # and font-size of 25
        Label(self.master, text="I have a font-size of 25",
              font=self.bold25).pack()
  
  
if __name__ == "__main__":
  
    # Instantiating top level
    root = Tk()
  
    # Setting the title of the window
    root.title("Change font-size of Label")
  
    # Setting the geometry i.e Dimensions
    root.geometry("400x250")
  
    # Calling our App
    app = App(root)
  
    # Mainloop which will cause this toplevel
    # to run infinitely
    root.mainloop()

输出: