0
第一代碼:'字典' 對象沒有屬性 'widgetName' 誤差的Tkinter python3
main.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'
這是爲什麼?我有一個預感,我可能需要更多的屬性= self.attribute的東西inits,但我不知道。這個結構讓我感到困惑。另外,風格是好的嗎?看起來好像我看起來很多人把他們的gui構建成一個功能。如果它很差,請提出替代方案。非常感謝你的幫助!
UPDATE1 我通過改變參數的順序遵循@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
UPDATE2
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'
UPDATE3 現在得到它下降到通過在d中添加父字符串而僅缺少一個arg,更改main.py以反映。看起來似乎想我也是通過一個自我,甚至砍斷我知道我不
將在早上嘗試,謝謝。 – fozbstuios
好吧,我試過了。它知道給出了這樣的錯誤: – fozbstuios
回溯(最近通話最後一個): 文件 「桌面/ kblMaker/main.py」,14號線在 對myApp =應用(d) 文件「/用戶/ fozbstudios /桌面/ kblMaker/self.createWidgets(self,inputDict) TypeError:createWidgets()需要2個位置參數,但有3個被給出 –
fozbstuios