我正在使用tkinter在python中创建一个简单的应用程序,其中包含一个菜单,我可以从中选择不同的选项并移动到新菜单。我正在使用Steven Vascellaro here给出的答案来消除框架,因为我在它们之间移动。在该程序的早期测试版本中,我能够为按钮提供自定义字体并使其正确显示,但是当我添加时 master,它在不同的帧之间切换,字体不再有效,只使按钮上的文字略大。
正常工作的代码版本是:
import tkinter as tk
from tkinter.font import Font, nametofont
class MainMenu(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
global myFont
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1, pad=50)
self.columnconfigure(0, weight=1)
self.resume = tk.Button(self, text='Continue', height=2, width=10, font=myFont, command=self.quit)
self.library = tk.Button(self, text='Library', height=2, width=10, command=self.quit)
self.resume.grid(row=1, column=0,sticky=tk.N+tk.E+tk.W)
self.library.grid(row=3, column=0,sticky=tk.E+tk.W)
root = tk.Tk()
global myFont
fontCheck = open("Options.txt","r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
app = MainMenu()
app.mainloop()
root.destroy() `
制作看似this
的菜单但是当我添加这个主部分时,它不再有效:
import tkinter as tk
from tkinter.font import Font, nametofont
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(MainMenu)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.grid()
class MainMenu(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
global myFont
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1, pad=50)
self.columnconfigure(0, weight=1)
self.resume = tk.Button(self, text='Continue', height=2, width=10, font=myFont, command=self.quit)
self.library = tk.Button(self, text='Library', height=2, width=10, command=self.quit)
self.resume.grid(row=1, column=0,sticky=tk.N+tk.E+tk.W)
self.library.grid(row=3, column=0,sticky=tk.E+tk.W)
root = tk.Tk()
global myFont
fontCheck = open("Options.txt","r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
app = Application()
app.mainloop()
root.destroy()
它会创建一个类似this
的菜单如果有人可以解释为什么字体在各帧中无法正常工作并解释我如何解决这个问题,我很乐意。
答案 0 :(得分:1)
您正在创建2个tk.Tk实例:一个用于设置字体,另一个用于应用程序。这两个实例不共享字体。解决方案是在Application类中设置Font(作为一种方法,很可能在初始化时)。
class Application(tk.Tk):
def __init__(self, *args, fontfile = None, **kw):
super().__init__(*args, **kw)
if fontfile is None: fontfile = "Options.txt"
self._frame = None
self.fontfile = fontfile
self.setupFont()
self.switch_frame(MainMenu)
def setupFont(self):
global myFont
with open(self.fontfile,"r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
其他一些注释: