我正在制作一个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'
帮助?
你确定你正在导入的'windows'是你认为它的文件吗?你可以在你的代码中加入'print(windows .__ file __)'来查看正在导入的文件。 –
你为什么将'self'传递给'calWin'? –
是的,我确定它是相同的文件,因为我用windows .__ doc__测试了它,并且它打印了我的文档。我将自己传递给calWin,因为我声明calWin是tk.Frame()类的一个实例,所以我想要的任何窗口小部件的父窗体实际上就是它自己,所以我把它放在了自己的位置。 – PySheep