2015-12-02 132 views
-1

我正在制作一个TKinter应用程序,并已开始使用类来构造我的代码。我想通过不在同一个文件中包含所有东西来保持可读性,但是当我在main_frontend.py中运行代码时发生此错误:AttributeError:'module'object has no attribute'calWin' 以下是main_frontend.py中的代码:AttributeError:'模块'对象没有属性'calWin'

import windows 
import tkinter as tk 
from tkinter import ttk 

class app(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 

     container.pack(side="top", fill="both", expand = True) 

     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 
     frame = windows.calWin(container, self) 
     self.frames[windows.calWin] = frame 
     frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(windows.calWin) 

    def show_frame(self, cont): 

     frame = self.frames[cont] 
     frame.tkraise() 

win = app() 
win.mainloop() 

这里是windows.py代码:

import passing 
import tkinter as tk 
from tkinter import ttk 

class calWin(tk.Frame): 

    def __init__(self, *args, **kwargs): 
     tk.Frame.__init__(self, *args, **kwargs) 
     calVar = StringVar() 
     calEnt = Entry(self, textvariable=calVar) 
     calEnt.pack() 
     calBut = Button(self, text='Calculate', command=lambda: passing.calPass(calVar.get(), calEnt, calLabel)) 
     calBut.pack() 
     calLabel = Label(self) 
     calLabel.pack() 

而且从passing.py:

import core 

def calPass(gtin, calEnt, calLabel): 
    if gtinValidate(gtin, 7): 
     calLabel.configure(text='The Full GTIN Number Is '+core.calculate(gtin)) 
     calEnt.delete(0, END) 
    else: 
     calLabel.configure(text='GTIN Invalid') 

而且从core.py:

def calculate(gtin): 
    ''' 
Calculates the check digit of a GTIN-8 number 
    ''' 
    x = (int(gtin[0])+int(gtin[2])+int(gtin[4])+int(gtin[6]))*3 
    x += int(gtin[1])+int(gtin[3])+int(gtin[5]) #Adds every other number in code 
    remainder = x%10 #Finds how far check digit is away from nearest multiple of ten 
    gtin = list(gtin) 
    if remainder != 0: 
     gtin.append(str(10-remainder))#Adds check digit to end of code if remainder is more than 0 
    else: 
     gtin.append('0') 
    return(''.join(gtin)) 

这里是我的错误的细节,当我从main_frontend.py运行代码:

Traceback (most recent call last): 
    File "H:\PythonCode\Stock Control Task\Organised GTIN TKinter GUI Project\main_frontend.py", line 33, in <module> 
    win = app() 
    File "H:\PythonCode\Stock Control Task\Organised GTIN TKinter GUI Project\main_frontend.py", line 22, in __init__ 
    frame = windows.calWin(container, self) 
AttributeError: 'module' object has no attribute 'calWin' 

帮助?

+0

你确定你正在导入的'windows'是你认为它的文件吗?你可以在你的代码中加入'print(windows .__ file __)'来查看正在导入的文件。 –

+0

你为什么将'self'传递给'calWin'? –

+0

是的,我确定它是相同的文件,因为我用windows .__ doc__测试了它,并且它打印了我的文档。我将自己传递给calWin,因为我声明calWin是tk.Frame()类的一个实例,所以我想要的任何窗口小部件的父窗体实际上就是它自己,所以我把它放在了自己的位置。 – PySheep

回答

0

既然你确实有calWinwindows,我猜你的问题与早期导入已经存在的字节码有关。导入语句首先检查模块是否已被导入。如果没有,它会检查模块是否已经被编译。尝试删除windows.pyc文件。或者在main_frontend.py文件中的import windows之后加reload(windows)

+0

不,不幸的是没有问题:/仍然是相同的错误 – PySheep

+0

回溯使得它看起来像引起错误的行是对构造函数的调用,但下面一行使用类本身作为你的字典中的一个键是可疑的。我认为,你永远不会得到第二个关键字。通过注释将错误分离到两条线之一,你能确认哪条线导致错误? –

+0

我可以确认这是对造成错误的构造函数的调用。当我注释掉下面这行时,我仍然得到相同的错误和相同的回溯:/这真的很奇怪 – PySheep

0

好吧,我只是重新写在一个单独的目录中的所有内容,并重新安装了Python,所有似乎都运行良好,不知道发生了什么。

相关问题