0

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.

cyberhoax
  • 1
  • 1
  • 7
  • There may be circular import issue because `first` import `second` and `second` import `first`. So it is better to pass the content of `text_url` to `second.get_search()` function so that `second` does not need to import `first`. – acw1668 Jun 08 '20 at 07:57

2 Answers2

0

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())
AfiJaabb
  • 316
  • 3
  • 11
  • If I remove If __name__ == "__main__" : I get this error AttributeError: module 'second' has no attribute 'get_search' – cyberhoax Jun 08 '20 at 07:55
  • Yes.The circular import is causing an error.Check my edit – AfiJaabb Jun 08 '20 at 08:03
  • I use your code but, now I can't see my buttons nor my entry widget – cyberhoax Jun 08 '20 at 08:20
  • 1
    Did you run the first script?Make sure that you run the second script.My code is made so that it works when you run the second script.If you want to make it work by running the first script @Finn has done it that way.Also if you want your window to run continuously his code is the one you are looking for. – AfiJaabb Jun 08 '20 at 08:25
  • yes but thank you too for responding the question, I really appreciate that – cyberhoax Jun 08 '20 at 08:31
0

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 commandof 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()
Finn
  • 2,333
  • 1
  • 10
  • 21
  • I was scratching my head since yesterday do you have any documentation or any article so I can read about this trick ? – cyberhoax Jun 08 '20 at 08:34
  • do you mean the lambda function for the button? I have no reference for the documentation, as I usually search stackoverflow for this. The answer I posted above + the comments are what I used in this. – Finn Jun 08 '20 at 08:55