首先是代码:
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'
这是为什么?我有一种预感,我需要在inits中做更多的attribute = self.attribute,但我不知道。那个结构让我很困惑。还有,风格好吗?看起来好像我似乎很多人将他们所有的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以反映。似乎它想要我传递自我,甚至霍夫我知道我不应该
答案 0 :(得分:2)
def __init__(inputDict,self, master=None):
这部分代码是错误的。在您的代码中,self
是您传入的参数,inputDict
将是该类的实例。你最好用:
def __init__(self, inputDict, master=None):