复制tkinter.font.font对象而不更改其族

2024-04-24 19:23:51 发布

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

如何复制tkinter.font.Font对象而不更改其family选项?在

下面是我使用过的脚本及其输出。我惊讶地发现它的.copy方法改变了字体的家族。在

测试脚本:

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkFont

class App(ttk.Frame):

    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        self._defaultFont=tkFont.Font(family='Times',size='11',weight='normal')        

        self.setFont()


    def setFont(self):
        """Customise Font styles""" 
        Font = self._defaultFont.copy()
        FontBold = self._defaultFont.copy()
        FontBold.config(weight='bold')
        print('_defaultFont.configure = ', self._defaultFont.configure())
        print('Font.configure         = ', Font.configure())
        print('Font.actual            = ', Font.actual())
        print('FontBold.configure     = ', FontBold.configure())
        print('FontBold.actual        = ', FontBold.actual())


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Test')
    root.geometry('200x100')
    app = App(root)
    app.pack(expand=1, fill='both')
    root.mainloop()

输出:

^{pr2}$

Tags: importselftkinterconfigureasrootparentprint
1条回答
网友
1楼 · 发布于 2024-04-24 19:23:51

我的问题中描述的问题发生在我使用了一个无效的字体系列,即tkinter.font.families()中不存在的字体来创建一个tkinter.font.Font()对象。因此需要确保tkinter.font.families()字体与.copy()方法一起使用。在

为了证明这一点,我研究了使用诸如'Times''Helvetica'这样的字体的结果,这两种字体不存在于我的ubuntu16.04.3系统中,以及使用'Ubuntu'和{}等字体的结果。我的调查结果如下所示。它们表明,tkinter.font.Font类的.copy()方法只有在tkinter.font.families()报告使用的字体时,才会完全复制字体的属性。下面是我用来调查我的问题的修改后的脚本。在

如果字体tkFont.families公司()未使用:

_defaultFont.configure =  {'family': 'Times', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'normal'}
_defaultFont.actual    =  {'family': 'Nimbus Roman No9 L', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'normal'}
Font.configure         =  {'family': 'Nimbus Roman No9 L', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'normal'}
Font.actual            =  {'family': 'Nimbus Roman No9 L', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'normal'}
FontBold.configure     =  {'family': 'Nimbus Roman No9 L', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'bold'}
FontBold.actual        =  {'family': 'Nimbus Roman No9 L', 'size': 11, 'underline': 0, 'slant': 'roman', 'overstrike': 0, 'weight': 'bold'}

_defaultFont.configure =  {'family': 'Helvetica', 'underline': 0, 'size': 11, 'weight': 'normal', 'slant': 'roman', 'overstrike': 0}
_defaultFont.actual    =  {'family': 'Nimbus Sans L', 'underline': 0, 'size': 11, 'weight': 'normal', 'slant': 'roman', 'overstrike': 0}
Font.configure         =  {'family': 'Nimbus Sans L', 'underline': 0, 'size': 11, 'weight': 'normal', 'slant': 'roman', 'overstrike': 0}
Font.actual            =  {'family': 'Nimbus Sans L', 'underline': 0, 'size': 11, 'weight': 'normal', 'slant': 'roman', 'overstrike': 0}
FontBold.configure     =  {'family': 'Nimbus Sans L', 'underline': 0, 'size': 11, 'weight': 'bold', 'slant': 'roman', 'overstrike': 0}
FontBold.actual        =  {'family': 'Nimbus Sans L', 'underline': 0, 'size': 11, 'weight': 'bold', 'slant': 'roman', 'overstrike': 0}

如果字体tkFont.families公司()已使用:

^{pr2}$

修改测试脚本:

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkFont

class App(ttk.Frame):

    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        fontFamilies = tkFont.families()
        print('tkFont.families() = ', fontFamilies)

        testfont = 'URW Gothic L' #change string to the font you want to use
        for font in fontFamilies:
            if testfont == font:
                print('\n{} in tkFont.families()'.format(testfont))
                break

        self._defaultFont=tkFont.Font(family=testfont, size='11',
                                      weight='normal')        
        self.setFont()


    def setFont(self):
        """Customise Font styles""" 
    self.Font = self._defaultFont.copy()
    self.FontBold = self._defaultFont.copy()
    self.FontBold.config(weight='bold')
    print('\n_defaultFont.configure = ', self._defaultFont.configure())
    print('_defaultFont.actual    = ', self._defaultFont.actual())
    print('Font.configure         = ', self.Font.configure())
    print('Font.actual            = ', self.Font.actual())
    print('FontBold.configure     = ', self.FontBold.configure())
    print('FontBold.actual        = ', self.FontBold.actual())


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Test')
    root.geometry('200x100')
    app = App(root)
    app.pack(expand=1, fill='both')
    root.mainloop()

相关问题 更多 >