为什么我在 python 3 中无法调用“模块”对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18069716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:51:35  来源:igfitidea点击:

Why am I getting 'module' object is not callable in python 3?

pythonpython-3.xtkinterruntime-error

提问by fozbstuios

First, all relevant code

一、所有相关代码

main.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

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

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

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

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

采纳答案by Andrew Clark

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

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

  • Leave your import statement alone, and use myApp = app.app({"testFKey":[3,2,2]})inside of main.py
  • Replace import appwith from app import app, now appwill refer to the class and myApp = app({"testFKey":[3,2,2]})will work fine
  • 留下你的导入语句,并myApp = app.app({"testFKey":[3,2,2]})在 main.py 中使用
  • 替换import appfrom app import app,现在app将引用该类并且myApp = app({"testFKey":[3,2,2]})可以正常工作

回答by Tadeck

In main.pychange second line to:

main.py改变第二行:

from app import app

The issue is you have appmodule and appclass within it. But you are importing module, not the class from it:

问题是你有app模块和app类。但是您正在导入模块,而不是从中导入的类:

myApp = app({"testFKey": [3, 2, 2]})

(you can also instead replace "app" inside line above into "app.app")

(您也可以将app上面的“ app.app”内行替换为“ ”)

回答by abarnert

The problem, as both F.J and Tadeck already explained, is that appis the module app, and app.appis the class appdefined in that module.

正如 FJ 和 Tadeck 已经解释的那样,问题在于appmodule app,并且app.appapp在该模块中定义的类。

You can get around that by using from app import app(or, if you must, even from app import *), as in Tadeck's answer, or by explicitly referring to app.appinstead of just app, as in F.J's answer.

您可以通过使用from app import app(或者,如果必须的话,甚至from app import *)来解决这个问题,就像在 Tadeck 的回答中一样,或者通过在 FJ 的回答中明确引用app.app而不是仅仅引用app

If you rename the class to App, that won't magically fix anything—you will still have to either from app import Appor refer to app.App—but it will make the problem a whole lot more obvious. And make your code less confusing after you've fixed the problem, too.

如果您将类重命名为App,这不会神奇地解决任何问题——您仍然必须要么要么from app import App引用要么引用app.App,但这会使问题变得更加明显。并在解决问题后让您的代码不那么混乱。

This is part of the reason that PEP 8recommends that:

这是PEP 8建议:

Modules should have short, all-lowercase names.

Almost without exception, class names use the CapWords convention.

模块应该有简短的全小写名称。

几乎无一例外,类名都使用 CapWords 约定。

That way, there's no way to mix them up.

那样的话,就没有办法把它们混在一起了。