我正在制作TKinter应用程序,并已开始使用类来构建我的代码。我希望通过不将所有内容放在同一个文件中来保持可读性,但是当我在main_frontend.py中运行代码时会出现此错误:AttributeError:'module'对象没有属性'calWin' 这是main_frontend.py中的代码:
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 :(得分:0)
由于calWin
中有windows
,我猜测您的问题与早期导入中已存在的字节码有关。 import语句首先检查模块是否已导入。如果没有,则检查模块是否已经编译。尝试删除windows.pyc
文件。或者在reload(windows)
文件中import windows
之后添加main_frontend.py
。
答案 1 :(得分:0)
好的,我只是在一个单独的目录中重新编写了所有内容并重新安装了Python,所有内容似乎运行良好,不知道发生了什么。