【问题标题】:Python Circular dependencies, unable to link variable to other filePython循环依赖,无法将变量链接到其他文件
【发布时间】:2017-03-11 21:10:18
【问题描述】:

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

ma​​in.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()                                                                          

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

【问题讨论】:

    标签: python variables tkinter import circular-dependency


    【解决方案1】:

    import 机制旨在允许循环导入。但必须记住以下几点:

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

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

    将上述内容应用于您的配对,我假设gui.py 是启动脚本。 Python 将立即创建一个空模块对象作为 sys.modules['__main__']. Somain.pyshould importgui.pywithimport ma​​in 的值作为 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.entry until the function is called. I suggest addingentry = gui.entryas the first line of the function and usingentry` 在需要的两个地方。

    当 tem2.py 运行时,下面这对文件会根据需要运行。

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

    【讨论】:

      【解决方案2】:

      entry 的定义移动到第三个文件中,并将其导入两个文件中。

      【讨论】:

        【解决方案3】:

        您可以将条目传递给WebsiteChange

        def WebsiteChange(entry):
            website = entry.get()
        
        submit = Button(master, text="Submit", 
            command=lambda e=entry: main.WebsiteChange(e))
        

        【讨论】:

          猜你喜欢
          • 2019-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-04
          • 1970-01-01
          • 1970-01-01
          • 2021-08-21
          • 2021-05-01
          相关资源
          最近更新 更多