Python循环依赖项,无法将变量链接到其他文件 [英] Python Circular dependencies, unable to link variable to other file

查看:142
本文介绍了Python循环依赖项,无法将变量链接到其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,允许我通过tkinter应用程序直接编辑word文档。我试图将我的gui文件中的tkinter输入链接到我的主文件,以便我可以执行我的docx函数。当我尝试以这种方式执行我的代码时,它告诉我entry.get()中的条目没有定义。当我尝试从main导入它时,我收到一个循环导入错误。

I am working on a program that allows me to directly edit a word document through a tkinter application. I am trying to link the tkinter input from my gui file to my main file so that I can execute my docx functions. When I try to execute my code this way, it tells me that entry in entry.get() is not defined. When I try to import this from main, I receive a circular import error.

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()                                                                          



<我一直在努力理解这个概念。循环错误令我头疼。任何帮助将不胜感激。

I have been struggling to understand this concept for awhile. Circular errors are giving me a headache. Any help would be greatly appreciated.

推荐答案

设计 import 机制允许循环进口。但必须记住以下内容:

The import mechanism is designed to allow circular imports. But one must remember the following:


  1. 从启动脚本创建的主模块的名称是 __main __ ,而不是文件名减去 .py 。导入启动脚本的任何其他文件必须 import __main __ ,而不是 import filename 。 (否则,将使用其正常名称创建基于启动脚本的第二个模块。)

  1. The name of the main module created from the startup script is __main__, rather than as its filename minus .py. Any other file importing the startup script must import __main__, not import filename. (Otherwise, a second module based on the startup script will be created with its normal name.)

模块中的代码执行暂停每个进口。初始导入的顺序很重要,因为链中的最后一个模块是第一个完成运行的模块。执行引用时,模块中的每个对象都必须可用。函数定义中的引用在导入过程中不会执行。

Execution of the code in a module is paused at each import. The order of initial imports is important as the last module in the chain is the first to be run to completion. Each object within modules must be available when the reference is executed. References within function definitions are not executed during the import process.

将上述内容应用于您的对,我假设 gui.py 是启动脚本。 Python将立即创建一个空模块对象作为 sys.modules ['__ main__']的值。所以 main.py 应导入 gui.py import main as gui (名称更改只是为了方便起见)。在函数定义中,您应该可以使用 gui.entry 而不会出现问题,因为不会尝试查找 gui.entry 直到调用该函数。我建议添加 entry = gui.entry 作为函数的第一行,并在所需的两个地方使用 entry`。

Applying the above to your pair, I assume that gui.py is the startup script. Python will immediate create an empty module object as the value of sys.modules['__main__']. Somain.pyshould importgui.pywithimport main as gui(the name change is just for convenience). Within the function definition, you should be able to usegui.entrywithout problem since there will be no attempt to lookupgui.entryuntil the function is called. I suggest addingentry = gui.entryas the first line of the function and usingentry` in the two places needed.

当运行tem2.py时,以下文件对按需运行。

The following pair of files run as desired when tem2.py is run.

# tem2.py
import tem3
a = 3
print(tem3.f())

# tem3.py
import __main__ as tem2
def f():
  return tem2.a

这篇关于Python循环依赖项,无法将变量链接到其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆