當我運行這段代碼:AttributeError的時候試圖用tkFont
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()
我得到:
Traceback (most recent call last):
File "Statify.py", line 23, in <module>
statify = Statify()
File "Statify.py", line 7, in __init__
self.titleFont = tkFont.Font(family='Helvetica', size=24, weight='bold')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/tkFont.py", line 88, in __init__
AttributeError: 'NoneType' object has no attribute 'tk'
從我讀過的東西,這應該工作,以及使用選項文件而不是沒有按沒有什麼不同。
Python版本2.7.2 Tkinter的優化版本8.5
這是整個代碼? – karthikr
我很快解釋了一個更大的應用程序的破碎部分,並因此引入了一些語法錯誤(其中一些是您編輯的 - 歡呼!)。 Brionius的回答爲我工作。謝謝! – insiduoustek