我正在嘗試設計一個視窗,上面有一個帶有控制元件的選單欄Add Client,Print Client Lists并且Exit. 我已經從 tkinter 庫中匯入了所有必需的元素并為控制元件創建了一個新容器,但是當我嘗試將第一個選單項添加到選單欄時出現此錯誤for k, v in cnf.items():AttributeError: 'str' object has no attribute 'items'
代碼
##this is the order.py file responsible for drawing
##the window and ui
from tkinter import *
from Business import *
def add_user():
print("hello world")
##create a container to hold the components inside the frame
myframe=Tk()
##create the menu bar
menub=Menu(myframe,background='#111', foreground='#111')
##the line below has a bug, i havwe defined the command and the name
menub.add_command('Add Client',command=myframe.quit)
myframe.config(menu=menub)
myframe.mainloop()
uj5u.com熱心網友回復:
你應該更好地閱讀教程 - 你必須使用 label=...
menub.add_command(label='Add Client', command=myframe.quit)
如果您不使用,label=那么它會將文本分配給函式定義中的第一個變數cnf=...- 這個變數需要dictionary并且它不知道如何處理 string 'Add Client'。您甚至可以cnf.items()在錯誤訊息中看到。
最小作業示例:
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def add_user():
print("hello world")
# --- main ---
root = tk.Tk()
menu = tk.Menu(root)
menu.add_command(label='Add Client', command=add_user)
menu.add_command(label='Exit', command=root.destroy) # `root.quit` may not close window in some situations
root.config(menu=menu)
root.mainloop()
PEP 8 -- Python 代碼風格指南
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352495.html
