最初のコード:
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'
どうしてこれなの?初期設定でより多くの attribute = self.attribute を行う必要があるという予感がありますが、わかりません。その構造は私を混乱させます。また、スタイルは良いですか?多くの人がすべてのGUI構築を1つの関数に任せているようには見えません。それが悪い場合は、代替案を提案してください。助けてくれてどうもありがとう!
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 に親文字列を追加することで、引数を 1 つだけ欠落させ、main.py を反映するように変更しました。私も自分自身を渡すことを望んでいるようですが、私はすべきではないことを知っています