为什么我在 python 3 中无法调用“模块"对象? [英] Why am I getting 'module' object is not callable in python 3?

查看:92
本文介绍了为什么我在 python 3 中无法调用“模块"对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,所有相关代码

ma​​in.py

import string
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]})

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(*v[1:])
                elif v[0] ==4:
                    tempDict[k] = dynamic_keys(k,*v[1:])
                elif  v[0]==5:
                    tempDict[k] = key_labels(*v[1:])
        for o in tempDict:
            tempDict[o].grid()
        return tempDict

static_keys.py

import tkinter
class static_keys(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')

现在来描述问题.当我在 python3 中运行 main.py 时,出现错误

Now for a description of the problem. When I run main.py in python3, I get the error

File "Desktop/kblMaker/main.py", line 13, in <module>
myApp =app({"testFKey":[3,2,2]})
TypeError: 'module' object is not callable

推荐答案

您有一个名为 app 的模块,其中包含一个名为 app 的类.如果你只是在 main.py 中执行 import app 那么 app 将引用模块,而 app.app 将引用类.这里有几个选项:

You have a module named app that contains a class named app. If you just do import app in main.py then app will refer to the module, and app.app will refer to the class. Here are a couple of options:

  • 不要管你的导入语句,在 main.py 中使用 myApp = app.app({"testFKey":[3,2,2]})
  • import app 替换为 from app import app,现在 app 将引用类和 myApp = app({"testFKey":[3,2,2]}) 会正常工作
  • Leave your import statement alone, and use myApp = app.app({"testFKey":[3,2,2]}) inside of main.py
  • Replace import app with from app import app, now app will refer to the class and myApp = app({"testFKey":[3,2,2]}) will work fine

这篇关于为什么我在 python 3 中无法调用“模块"对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆