0

首先是代码:

主文件

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})

应用程序.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

静态键.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 构造任务都集中到一个函数中。如果它很差,请提出替代方案。非常感谢你的帮助!

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

更新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'

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

4

1 回答 1

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

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

def __init__(self, inputDict, master=None):
于 2013-08-06T02:25:06.440 回答