尝试使用tkFon时发生AttributeError

2024-10-01 17:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

运行此代码时:

from Tkinter import *
import tkFont

class Statify():

    def __init__(self):

        ### Broken
        self.titleFont = tkFont.Font(family='Helvetica', size=24, weight='bold')
        self.option_add(*Label*font, self.titleFont)
        ###

        self.root = Tk()
        self.root.withdraw()
        self.main = Toplevel(self.root)
        self.main.title('')
        self.main_header = Frame(self.main)
        self.main_footer = Frame(self.main)
        self.main_title = Label(self.main_header, text='Statify Me v1.0 (WIP)')
        self.main_exit = Button(self.main_footer, text='Quit', command=quit)
        self.main_header.pack()
        self.main_footer.pack()
        self.main_title.pack()
        self.main_exit.pack()
        mainloop()

statify = Statify()

我得到:

^{pr2}$

从我所读到的,这应该是有效的,而使用选项文件并没有什么区别。在

Python 2.7.2版 Tkinter版本8.5


Tags: textimportselftitlemaintkinterrootframe
1条回答
网友
1楼 · 发布于 2024-10-01 17:36:46

如果您查看docs for tkFont,您会发现问题是tkFont.Font需要一个root参数,即父小部件。通过将调用移动到创建根窗口的下面的tkFont.Font来解决此问题,然后添加self.root作为关键字参数,如下所示:

self.root = Tk()
self.titleFont = tkFont.Font(root=self.root, family='Helvetica', size=24, weight='bold')
                             ^^^^^^^^^^^^^^

您还没有遇到这个bug,但是下一行有问题-我想您是想写self.root.option_add而不是{},我不知道您想如何处理*Label*font业务。在

相关问题 更多 >

    热门问题