[英]Tkinter font size not changing
TFont = ("Verdana", 36) # Changes font
Currently, I am trying to change the font size for a GUI in Python.目前,我正在尝试更改 Python 中 GUI 的字体大小。 No matter what I change here, the font size in the window stays the same.
无论我在这里更改什么,窗口中的字体大小都保持不变。
Here is how my code looks currently:这是我的代码目前的样子:
TFont = ("Verdana", 36) # Change font
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default = "GUIIconICO.ico")
tk.Tk.wm_title(self, "Graduate Technical Project")
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (TitleScreen, PlayerPage, SessionPage, PlayerPosition):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(TitleScreen)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class TitleScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Home Page", font = "TFont")
label.pack(side = "top")
button1 = ttk.Button(self, text = "Player", command = lambda : controller.show_frame(PlayerPage))
button1.pack(side = tk.LEFT)
button6 = ttk.Button(self, text = "Session", command = lambda : controller.show_frame(SessionPage))
button6.pack(side = tk.LEFT)
button10 = ttk.Button(self, text = "Player Position", command = lambda : controller.show_frame(PlayerPosition))
button10.pack(side = tk.LEFT)
Could anyone help?有人能帮忙吗?
Solved, changed TFont from a string to a variable as recommended已解决,按照建议将 TFont 从字符串更改为变量
label = tk.Label(self, text = "Home Page", font =
"TFont")label = tk.Label(self, text = "Home Page", font =
"TFont")label = tk.Label(self, text = "Home Page", font = TFont )
label = tk.Label(self, text = "主页", font = TFont )
Whilst not listed in this query, TFont was also inside of a parent class causing an error.虽然未在此查询中列出,但 TFont 也在父类内部导致错误。 Placing the TFont variable outside this class seemed to fix this.
将 TFont 变量放在此类之外似乎可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.