Python循环依赖,无法link变量到其他文件
Python Circular dependencies, unable to link variable to other file
我正在开发一个允许我通过 tkinter 应用程序直接编辑 word 文档的程序。我正在尝试 link 从我的 gui 文件到我的主文件的 tkinter 输入,以便我可以执行我的 docx 函数。当我尝试以这种方式执行我的代码时,它告诉我 entry.get() 中的条目未定义。当我尝试从 main 导入它时,我收到一个循环导入错误。
main.py
from docx import Document
from docx.shared import Inches
import os
os.chdir("\Users\insanepainz\Desktop")
doc = Document('TemplateTest.docx')
paragraphs = doc.paragraphs
def WebsiteChange():
website = entry.get()
print(website)
master.quit()
for paragraph in doc.paragraphs:
if '^website' in paragraph.text:
paragraph.text = gui.entry
print(paragraph.text)
doc.save(doc)
pass
gui.py
import main
from tkinter import *
master = Tk()
#------------Web Entry Window
Label(master, text="Website Name: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
submit = Button(master, text="Submit", command=main.WebsiteChange)
submit.grid(row=1)
# Centers the program window
master.eval('tk::PlaceWindow %s center' % master.winfo_pathname(master.winfo_id()))
mainloop()
一段时间以来,我一直在努力理解这个概念。循环错误让我很头疼。任何帮助将不胜感激。
将 entry
的定义移动到第三个文件中,并将其导入到两个文件中。
您可以将条目传递给 WebsiteChange
:
def WebsiteChange(entry):
website = entry.get()
submit = Button(master, text="Submit",
command=lambda e=entry: main.WebsiteChange(e))
import
机制旨在允许循环导入。但必须记住以下几点:
从启动脚本创建的主模块的名称是 __main__
,而不是其文件名减去 .py
。导入启动脚本的任何其他文件必须 import __main__
,而不是 import filename
。 (否则,将使用其正常名称创建基于启动脚本的第二个模块。)
模块中代码的执行在每个 import
处暂停。初始导入的顺序很重要,因为链中的最后一个模块是第一个 运行 完成的。执行引用时,模块中的每个对象都必须可用。在导入过程中不执行函数定义中的引用。
将以上内容应用于您的对,我假设 gui.py
是启动脚本。 Python 将立即创建一个空模块对象作为 sys.modules['__main__']. So
main.pyshould import
gui.pywith
import main[= 的值42=] as gui(the name change is just for convenience). Within the function definition, you should be able to use
gui.entrywithout problem since there will be no attempt to lookup
gui.entryuntil the function is called. I suggest adding
entry = gui.entryas the first line of the function and using
entry` 在需要的两个地方。
当tem2.py为运行.
时,运行根据需要生成以下一对文件
# tem2.py
import tem3
a = 3
print(tem3.f())
# tem3.py
import __main__ as tem2
def f():
return tem2.a
我正在开发一个允许我通过 tkinter 应用程序直接编辑 word 文档的程序。我正在尝试 link 从我的 gui 文件到我的主文件的 tkinter 输入,以便我可以执行我的 docx 函数。当我尝试以这种方式执行我的代码时,它告诉我 entry.get() 中的条目未定义。当我尝试从 main 导入它时,我收到一个循环导入错误。
main.py
from docx import Document
from docx.shared import Inches
import os
os.chdir("\Users\insanepainz\Desktop")
doc = Document('TemplateTest.docx')
paragraphs = doc.paragraphs
def WebsiteChange():
website = entry.get()
print(website)
master.quit()
for paragraph in doc.paragraphs:
if '^website' in paragraph.text:
paragraph.text = gui.entry
print(paragraph.text)
doc.save(doc)
pass
gui.py
import main
from tkinter import *
master = Tk()
#------------Web Entry Window
Label(master, text="Website Name: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
submit = Button(master, text="Submit", command=main.WebsiteChange)
submit.grid(row=1)
# Centers the program window
master.eval('tk::PlaceWindow %s center' % master.winfo_pathname(master.winfo_id()))
mainloop()
一段时间以来,我一直在努力理解这个概念。循环错误让我很头疼。任何帮助将不胜感激。
将 entry
的定义移动到第三个文件中,并将其导入到两个文件中。
您可以将条目传递给 WebsiteChange
:
def WebsiteChange(entry):
website = entry.get()
submit = Button(master, text="Submit",
command=lambda e=entry: main.WebsiteChange(e))
import
机制旨在允许循环导入。但必须记住以下几点:
从启动脚本创建的主模块的名称是
__main__
,而不是其文件名减去.py
。导入启动脚本的任何其他文件必须import __main__
,而不是import filename
。 (否则,将使用其正常名称创建基于启动脚本的第二个模块。)模块中代码的执行在每个
import
处暂停。初始导入的顺序很重要,因为链中的最后一个模块是第一个 运行 完成的。执行引用时,模块中的每个对象都必须可用。在导入过程中不执行函数定义中的引用。
将以上内容应用于您的对,我假设 gui.py
是启动脚本。 Python 将立即创建一个空模块对象作为 sys.modules['__main__']. So
main.pyshould import
gui.pywith
import main[= 的值42=] as gui(the name change is just for convenience). Within the function definition, you should be able to use
gui.entrywithout problem since there will be no attempt to lookup
gui.entryuntil the function is called. I suggest adding
entry = gui.entryas the first line of the function and using
entry` 在需要的两个地方。
当tem2.py为运行.
时,运行根据需要生成以下一对文件# tem2.py
import tem3
a = 3
print(tem3.f())
# tem3.py
import __main__ as tem2
def f():
return tem2.a