【问题标题】:'dict' object has no attribute 'widgetName' error tkinter python3'dict' 对象没有属性 'widgetName' 错误 tkinter python3
【发布时间】:2013-08-06 02:21:22
【问题描述】:

首先是代码:

ma​​in.py

import string
from app import App
group1=[ "spc", "bspc",",","."]#letters, space, backspace(spans mult layers)
# add in letters one at a time
for s in string.ascii_lowercase:
    group1.append(s)
group2=[0,1,2,3,4,5,6,7,8,9, "tab ","ent","lAR" ,"rAR" , "uAR", "dAR"]
group3= []
for s in string.punctuation:
    group3.append(s)#punc(spans mult layers)
group4=["copy","cut","paste","save","print","cmdW","quit","alf","sWDW"] #kb shortcut
masterGroup=[group1,group2,group3,group4]
myApp =App({"testFKey":[3,2,2,None})

app.py

import tkinter as tk
import static_keys
import dynamic_keys
import key_labels
class App(tk.Frame):

def __init__(inputDict,self, master=None):
    tk.Frame.__init__(self, master)
    self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
    self.createWidgets(self, inputDict)
def createWidgets(self,inDict):
    top=self.winfo_toplevel()
    top.rowconfigure(0, weight=1)
    top.columnconfigure(0, weight=1)
    self.rowconfigure(0, weight=1)
    self.columnconfigure(0, weight=1)
    tempDict = {}
    for k,v in inDict.items():
            if 1<=v[0]<=3:
                tempDict[k] = static_keys(k,*v[1:])
            elif v[0] ==4:
                tempDict[k] = dynamic_keys(k,*v[1:])
            elif  v[0]==5:
                tempDict[k] = key_labels(k,*v[1:])
    for o in tempDict:
        tempDict[o].grid()
    return tempDict

static_keys.py

import tkinter
class StaticKeys(tkinter.Label):
    """class for all keys that just are initiated then do nothing
    there are 3 options
    1= modifier (shift etc)
    2 = layer
    3 = fkey, eject/esc"""
    def __init__(t,selector,r,c, parent,self):
        if selector == 1:
            tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#676731')
        if selector == 2:
            tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#1A6837')
        if selector == 3:
            tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#6B6966')

当我运行 main.py 时,我得到:

Traceback (most recent call last):
  File "Desktop/kblMaker/main.py", line 13, in <module>
    myApp =App({"testFKey":[3,2,2]})
  File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 8, in __init__
    tk.Frame.__init__(self, master)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2574, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2067, in __init__
    self.widgetName = widgetName
AttributeError: 'dict' object has no attribute 'widgetName'

这是为什么?我有一种预感,我可能需要在 inits 中做更多的属性 = self.attribute 的东西,但我不知道。这种结构让我感到困惑。还有,画风好不好?似乎我似乎没有很多人将他们所有的 gui 构造任务都集中到一个函数中。如果它很差,请提出替代方案。非常感谢您的帮助!

更新1 我听从@zhangyangyu 的建议,改变了参数的顺序,得到了:

Traceback (most recent call last):
  File "Desktop/kblMaker/main.py", line 14, in <module>
    myApp =App(d)
  File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 10, in __init__
    self.createWidgets(self, inputDict)
TypeError: createWidgets() takes 2 positional arguments but 3 were given

更新2

Traceback (most recent call last):
  File "Desktop/kblMaker/main.py", line 14, in <module>
    myApp =App(d)
  File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 10, in __init__
    self.createWidgets(inputDict)
  File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 20, in createWidgets
    tempDict[k] = StaticKeys(k,*v[1:])
TypeError: __init__() missing 2 required positional arguments: 'parent' and 'self'

更新3 现在通过在 d 中添加父字符串将其归结为仅缺少一个 arg,更改 main.py 以反映。好像它也想让我通过一个自我,即使我知道我不应该

【问题讨论】:

    标签: python dictionary python-3.x tkinter


    【解决方案1】:
    def __init__(inputDict,self, master=None):
    

    您的这部分代码是错误的。在您的代码中,self 是您传入的参数,inputDict 将是该类的实例。你最好使用:

    def __init__(self, inputDict, master=None):
    

    【讨论】:

    • Traceback(最近一次调用最后):文件“Desktop/kblMaker/main.py”,第 14 行,在 myApp =App(d) 文件“/Users/fozbstudios/Desktop/kblMaker /app.py",第 10 行,在 init self.createWidgets(self, inputDict) TypeError: createWidgets() 接受 2 个位置参数,但给出了 3 个
    • self.createWidgets(self, inputDict) 应该是 self.createWidgets(inputDict)self 是隐式传递的,你没有传递它。我建议对 Python 类进行评论。
    • 已修复。现在得到 Traceback(最近一次调用最后一次):文件“Desktop/kblMaker/main.py”,第 14 行,在 myApp =App(d) 文件“/Users/fozbstudios/Desktop/kblMaker/app .py”,第 10 行,在 init self.createWidgets(inputDict) 文件“/Users/fozbstudios/Desktop/kblMaker/app.py”,第 20 行,在 createWidgets tempDict[k] = StaticKeys( k,*v[1:]) TypeError: __init__() 缺少 2 个必需的位置参数:'parent' 和 'self'
    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多