Python 如何将值传递给另一个文件,该文件本身已导入当前文件?
我有两个文件,一个包含tkinter代码,另一个包含函数。我在tkinter窗口中有一个按钮和一个Python 如何将值传递给另一个文件,该文件本身已导入当前文件?,python,tkinter,python-import,Python,Tkinter,Python Import,我有两个文件,一个包含tkinter代码,另一个包含函数。我在tkinter窗口中有一个按钮和一个条目字段。我试图在单击按钮时执行该功能,但它需要条目字段中的文本才能工作。尝试从tkinter文件导入任何内容时出错: tkinter_file.py: import File window = Tk() def input(): s = entry1.get() return s entry1 = Entry(window) button1 = Button(window, te
条目
字段。我试图在单击按钮时执行该功能,但它需要条目
字段中的文本才能工作。尝试从tkinter文件导入任何内容时出错:
tkinter_file.py:
import File
window = Tk()
def input():
s = entry1.get()
return s
entry1 = Entry(window)
button1 = Button(window, text='GO', command=File.function)
File.py:
from tkinter import *
import tkinter_file
def function():
req_url = 'http://someurl.com/{}'.format(tkinter_file.input)
requests get url etc. etc.
当我将tkinter_文件
导入file.py
,甚至只导入函数input
,我似乎就收到了一个错误:
File "/etc/etc/tkinter_file.py", line 75, in <module>
button1 = Button(window, text='GO', command=File.function)
AttributeError: module 'File' has no attribute 'function'
文件“/etc/etc/tkinter_File.py”,第75行,在
button1=按钮(窗口,text='GO',command=File.function)
AttributeError:模块“文件”没有“函数”属性
我认为问题在于
req\u url
没有值s
,也可能是将这两个文件相互导入,但是如何克服这个问题呢?如果有两个模块,比如a.py
和b.py
,您不能在a
中导入模块b
,然后在b
中导入模块a
,因为这会创建一个循环的依赖关系,无法明确解决
解决方案是将该函数正常运行所需的内容作为参数传递给File.function
,即entry1
的内容
button1 = Button(window, text='GO', command=lambda: File.function(entry1.get()))
如果您有两个模块,例如
a.py
和b.py
,则不能在a
中导入模块b
,然后在b
中导入模块a
,因为这会创建一个循环的依赖关系,无法明确解决
解决方案是将该函数正常运行所需的内容作为参数传递给File.function
,即entry1
的内容
button1 = Button(window, text='GO', command=lambda: File.function(entry1.get()))