简体   繁体   中英

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

like I have a file 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()

and I have second file name second.py

from first import *

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

print(get_search())

I want to access text_url from first.py in my second.py, but when i run this code

NameError: name 'text_url' is not defined

I get this error can anyone help me to understand what's wrong is it the issue due to variable scope or due tkinter? cause without tkinter I can call the variable easily but with tkinter i just can't.

In your first python script you have included if __name__ == "__main__": . The code within this if statement will not be executed when you have imported the first script in the second script as it is not the main script. Try removing the if statement.

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

EDIT

Ok I tried your code myself and it appears that the circular import is causing some problems.So i restructured your code like below.

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

second.py

from first import get_url, text_url

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

print(get_search())

If you want to access the variable in another script you have to pass it as an argument. The fact that theyare both called text_url does not matter to the scripts, they are independent. They can have different names, they only have to have the same name inside each script.

I assume you start the first.py script rigt? As @AfiJaabb said, you should not import first in second and second in first (circular importing).

# 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

It is a little trickier than usual as you have to pass inside the command of the button. In the script you would just use second.get_search(text_url) , but for the button you have to make it a lambda function (explained here ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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