python - 为什么我的字体在 tkinter 中无法正确显示?

标签 python python-3.x tkinter

我正在使用 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 的菜单

如果有人能够解释为什么字体在框架中无法正常工作并解释如何解决此问题,我会很高兴。

最佳答案

您正在创建 2 个 tk.Tk 实例:一个用于设置字体,另一个用于您的应用程序。这两个实例不共享字体。解决方案是在您的应用程序类中设置字体(可能作为一种方法,并且最有可能在初始化时)。

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

其他一些注意事项:

  • 我不太喜欢使用全局;将其作为应用程序的属性或作为 ttk.Style 会更好(在我看来)。
  • 您可能需要考虑使用具有预定义结构(推荐 json)的选项文件,以便以更明确的方式读取该结构。

关于python - 为什么我的字体在 tkinter 中无法正确显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50415969/

相关文章:

python - 迭代多维数组并搜索形成正方形的点

python - 运行子方法而不是父方法?

python - 错误 : could not create '/Library/Python/2.7/site-packages/xlrd' : Permission denied

python - 在按关注者数量排名的推文中找到主题的好算法?

python 3 : class "template" (function that returns a parameterized class)

python - Tkinter plt.figure() 不绘图,但Figure() 绘图

python - 在编辑之前使用 python 脚本将 Windows 注册表备份到文件

python - 使用 python 请求模块时出现 HTTP 503 错误

python - 打开文件 (Tkinter)

python - tkinter 执行在大约 140 次迭代后终止,没有错误消息(内存泄漏?)