2017-08-16 62 views
1

我正在使用tkinter devoloping一個桌面應用程序。設置字體引發異常。tkinter字體中沒有屬性「call」錯誤

tmp.py

def main(root): 
    frame = Frame(root.master) 
    font = Font(size=25 , weight="bold") 
    label = Label(frame , font=font , text="tuna fish") 
    label.pack() 
    frame.pack() 

這是驅動器程序main.py main.py

if __name__ == "__main__": 
    root = start.baseApp() 
    root.Menu_Customer.add_command(label="New customer", command=lambda: tmp.main(root=root)) 
    root.master.mainloop() 

baseApp

我已創建根窗口並帶有名稱的菜單欄客戶並添加菜單項新客戶main.py
我得到在tmp.py異常說

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib64/python3.5/tkinter/__init__.py", line 1559, in __call__ 
    return self.func(*args) 
    File "main.py", line 10, in <lambda> 
    root.Menu_Customer.add_command(label="New customer", command=lambda: tmp.main(root=root)) 
    File "/home/engle/Documents/Project/CleanMaster/tmp.py", line 6, in main 
    font = Font(size=25 , weight="bold") 
    File "/usr/lib64/python3.5/tkinter/font.py", line 93, in __init__ 
    tk.call("font", "create", self.name, *font) 
AttributeError: 'NoneType' object has no attribute 'call' 


什麼錯呢?

+0

您是否包含此行:'import tmp'? –

+0

當然是男人。我輸入了。 – deepak

+3

我不能運行你的代碼,因此不能排除它的故障,請檢查這一點,並相應地修改https://stackoverflow.com/help/mcve –

回答

0

爲了在tkinter中使用Font類,an instance of Tk() must be running。如果您正在運行此類實例,請嘗試將它明確地作爲參數傳遞給您的字體:

def main(root): 
    ... 
    font = Font(root=root.master, size=25 , weight="bold") 
    ... 
相關問題