如何复制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}$
我的问题中描述的问题发生在我使用了一个无效的字体系列,即
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公司()未使用:
如果字体tkFont.families公司()已使用:
^{pr2}$修改测试脚本:
相关问题 更多 >
编程相关推荐