如何增加 Tk 的标签字体大小 (Python)

问题描述

我想增加 GUI 的标签字体大小。我尝试了下面附加的方式。但这会引发错误。到目前为止我尝试的是下面的代码错误

Traceback (most recent call last):
  File "C:/Users/kobinath/PycharmProjects/pythonProject4/ex.py",line 123,in <module>
    style = root.Style()
  File "C:\Users\kobinath\AppData\Local\Programs\Python\python38-32\lib\tkinter\__init__.py",line 2345,in __getattr__
    return getattr(self.tk,attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'Style**
root = Tk()
style = root.Style()
style.configure(
   "BW.TLabel",foreground="black",background="white",font="Helvetica",fontsize=12
   )
    
tk.Label(root,text="Student ID",style="BW.TLabel").place(x=400,y=5)

解决方法

您可以在 tkinter 标签中使用 fg、bg 和字体参数,如以下代码所示:

import tkinter as tk

root = tk.Tk()

tk.Label(root,text="Student ID",fg= "black",bg="white",font=("Helvetica",12)).place(x=400,y=5)

root.mainloop()