所以我有2个要导入的python文件,因此我想将文件1导入文件2,将文件2导入文件1。但是当我这样做时,出现此错误。
Traceback (most recent call last):
File "C:\Users\User\Desktop\Bank\Programs\Main_File.py", line 1, in <module>
from Login import *
File "C:\Users\User\Desktop\Bank\Programs\Login.py", line 2, in <module>
from Main_File import *
File "C:\Users\User\Desktop\Bank\Programs\Main_File.py", line 12, in <module>
Login(var)
NameError: name 'Login' is not defined
这是我的代码:
主要代码:
from Login import *
from time import *
from tkinter import *
#Frame Setup########
root = Tk()
root.configure(bg="SeaGreen")
var = Frame(root)
var.configure(bg=var.master['bg'])
var.grid()
Login(var)
def RegClicked():
print("Hi")
次级代码:
from tkinter import *
from Main_File import *
def Login(Log):
#First Name#########
L1 = Label(Log, text="First Name:")
L1.configure(font=("Courier", 35),bg=L1.master['bg'], fg="Gold")
L1.grid(row=0, column=0, padx=(50, 0), pady=(100, 0))
FirstN = Entry(Log)
FirstN.configure(width=50)
FirstN.grid(row=0, column=1, pady=(100, 0))
#Last Name##########
L2 = Label(Log, text="Last Name:")
L2.configure(font=("Courier", 35),bg=L2.master['bg'], fg="Gold")
L2.grid(row=1, column=0, padx=(50, 0), sticky=W)
LastN = Entry(Log)
LastN.configure(width=50)
LastN.grid(row=1, column=1)
#Login Button#######
LogB = Button(Log, text="Login", command = lambda : LogClicked())
LogB.configure(font=("Courier", 15),bg=LogB.master['bg'], activebackground=LogB.master['bg'], width=25)
LogB.grid(row=3, column=1)
#Register Button####
RegB = Button(Log, text="Register", command = lambda : RegClicked())
RegB.configure(font=("Courier", 15),bg=RegB.master['bg'], activebackground=RegB.master['bg'], width=25)
RegB.grid(row=4, column=1)
当我从第二个文件中删除“ from Main_File import *”时,它工作正常,但是当我将其放入时,会导致错误。任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
为避免循环导入,应删除:
from Main_File import *
来自Login.py
,然后添加:
from Main_File import RegClicked
到Login
函数主体的第一行。