繁体   English   中英

如何使用 function 访问 python 文件中另一个 python 文件中的变量

[英]how to access the variable from other python file in another python file inside a function using tkinter

就像我有一个文件 first.py

from tkinter import *
import second

if __name__ == "__main__":

    root = Tk()


    text_url = StringVar()
    e1 = Entry(root, width=50, justify=CENTER, textvariable=text_url)
    e1.insert(0, 'Enter your URL')
    e1.pack(pady=(300, 0))
    b1 = Button(root, text="SEARCH", justify=CENTER, bg='#900c3f', width=12, fg="white", height=1, relief=GROOVE,font="verdana", command=second.get_search)
    b1.pack(pady=60)
    root.mainloop()

我有第二个文件名 second.py

from first import *

def get_search():
u = text_url.get()
return u

print(get_search())

我想从我的 second.py 中的 first.py 访问 text_url,但是当我运行此代码时

NameError: name 'text_url' is not defined

我收到此错误,任何人都可以帮助我了解问题是由于变量 scope 还是由于 tkinter 引起的问题? 因为没有 tkinter 我可以轻松调用变量,但使用 tkinter 我不能。

在您的第一个 python 脚本中,您已包含if __name__ == "__main__": 当您在第二个脚本中导入第一个脚本时,此 if 语句中的代码将不会执行,因为它不是主脚本。 尝试删除 if 语句。

from tkinter import *
import second



root = Tk()


text_url = StringVar()
e1 = Entry(root, width=50, justify=CENTER, textvariable=text_url)
e1.insert(0, 'Enter your URL')
e1.pack(pady=(300, 0))
b1 = Button(root, text="SEARCH", justify=CENTER, bg='#900c3f', width=12, fg="white", height=1, relief=GROOVE,font="verdana", command=second.get_search)
b1.pack(pady=60)
root.mainloop()

编辑

好的,我自己尝试了您的代码,似乎循环导入导致了一些问题。所以我像下面这样重组了您的代码。

第一个.py

from tkinter import *


def i_command():
    root.destroy()


root = Tk()
text_url = StringVar()

def get_url():
    e1 = Entry(root, width=50, justify=CENTER, textvariable=text_url)
    e1.insert(0, 'Enter your URL')
    e1.pack(pady=(300, 0))
    b1 = Button(root, text="SEARCH", justify=CENTER, bg='#900c3f', width=12, fg="white", height=1, relief=GROOVE,font="verdana", command=i_command)
    b1.pack(pady=60)
    root.mainloop()
    return text_url.get()

第二个.py

from first import get_url, text_url

def get_search():
    u = get_url()
    return u

print(get_search())

如果要访问另一个脚本中的变量,则必须将其作为参数传递。 它们都被称为text_url的事实与脚本无关,它们是独立的。 它们可以有不同的名称,它们只需要在每个脚本中具有相同的名称。

我假设您启动first.py脚本绑定? 正如@AfiJaabb 所说,您不应该first导入second ,然后再导入second first循环导入)。

# from first import * <-- remove this

def get_search(text_url_from_first): # here comes the variable from the other script
    u = text_url_from_first.get()
    return u

这比平常有点棘手,因为您必须在按钮的command内部传递。 在脚本中,您只需使用second.get_search(text_url) ,但对于按钮,您必须将其设为 lambda function (在此处解释):

from tkinter import *
import second

if __name__ == "__main__":

    root = Tk()
    text_url = StringVar()
    e1 = Entry(root, width=50, justify=CENTER, textvariable=text_url)
    e1.insert(0, 'Enter your URL')
    e1.pack(pady=(300, 0))
    b1 = Button(root, text="SEARCH", justify=CENTER, bg='#900c3f', width=12, fg="white", height=1, relief=GROOVE,font="verdana", command=lambda: second.get_search(text_url))
    b1.pack(pady=60)
    root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM