Python3 Tkinter字体不工作 [英] Python3 Tkinter fonts not working

查看:269
本文介绍了Python3 Tkinter字体不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用tkinter使用python 3.3,并安装了python3-tk软件包。在大多数文档中,使用了旧的import tkFont,这已经不再适用了。

这应该是可行的:

 从tkinter导入字体
appHighlightFont = font.Font(family ='Helvetica',size = 12,weight ='bold')
字体。 family()

然而,我在第二行得到这个异常:

  Traceback(最近一次调用的最后一个):
在< module>中的第1行中输入< stdin>
文件/usr/lib/python3.3/tkinter/font.py,第92行,在__init__
root.tk.call(font,create,self.name,*字体)
AttributeError:'NoneType'对象没有属性'tk'

a href =http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html =nofollow> http://infohost.nmt.edu/tcc/help/pubs/ tkinter / web / fonts.html 和 http://www.tkdocs.com/tutorial /fonts.html ,这是迄今为止最有用的tkinter文档。



不幸的是,我仍然无法弄清楚我做错了什么。

解决方案

您应该导入 font not fonts 。此外,如果您发布的代码是实际代码,则忽略在使用字体之前创建根窗口。您必须先创建一个根窗口。

 从tkinter导入字体
导入tkinter作为tk
.. 。
root = tk.Tk()
...
appHighlightFont = font.Font(family ='Helvetica',size = 12,weight ='bold')
字体.families()


I am using python 3.3 with tkinter, and the package python3-tk is installed. In most docs the old "import tkFont" is used, which is not working any more.

This is supposed to work:

from tkinter import font
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()

However, I get this exception on the second line:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/tkinter/font.py", line 92, in __init__
    root.tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'tk'

I checked http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html and http://www.tkdocs.com/tutorial/fonts.html which were the most useful tkinter docs so far.

Unfortunately I still can't figure out what I am doing wrong.

解决方案

You should import font not fonts. Also, if the code you posted is actual code, you are neglecting to create a root window before working with fonts. You must create a root window first.

from tkinter import font
import tkinter as tk
...
root = tk.Tk()
...
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()

这篇关于Python3 Tkinter字体不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆