如何将值传递给另一个文件,该文件本身已导入到当前文件中?
How do I pass a value to another file, which is itself imported in current file?
我有两个文件,一个包含 tkinter 代码,另一个包含函数。我在 tkinter window 和 Entry
字段中有一个按钮。我试图在单击按钮时执行该功能,但它需要 Entry
字段中的文本才能工作。尝试从 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
导入 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'
我认为 req_url
没有立即获得值 s
是问题所在,并且可能将 2 个文件相互导入,但你如何克服这个问题?
如果你有两个模块,比如说 a.py
和 b.py
,你不能在 a
中导入模块 b
然后再导入模块 a
在 b
中,因为这会创建一个 cyclic 依赖关系,无法明确解决!
一个解决方案是将您需要的函数作为参数传递给 File.function
以正确地传递给 运行,即 entry1
.
的内容
button1 = Button(window, text='GO', command=lambda: File.function(entry1.get()))
我有两个文件,一个包含 tkinter 代码,另一个包含函数。我在 tkinter window 和 Entry
字段中有一个按钮。我试图在单击按钮时执行该功能,但它需要 Entry
字段中的文本才能工作。尝试从 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
导入 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'
我认为 req_url
没有立即获得值 s
是问题所在,并且可能将 2 个文件相互导入,但你如何克服这个问题?
如果你有两个模块,比如说 a.py
和 b.py
,你不能在 a
中导入模块 b
然后再导入模块 a
在 b
中,因为这会创建一个 cyclic 依赖关系,无法明确解决!
一个解决方案是将您需要的函数作为参数传递给 File.function
以正确地传递给 运行,即 entry1
.
button1 = Button(window, text='GO', command=lambda: File.function(entry1.get()))