Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to share a variable between a library and a main script in python?

I have a main script script.py

from tkinter import *

root = Tk()

frame = Frame(root)
frame.pack()
textDisplay = Entry(frame)
textDisplay.pack()
btn1=Button(root,text='add here ',command=function)
btn1.pack()
def function ():
    textDisplay.insert(0,'Some String')
    return

root.mainloop()

i want to create a library that contain my functions (function()), so i create MyLib.py

from script.py import textDisplay
def function ():
    textDisplay.insert(0,'Some String')
    return

and i add this line to script.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from MyLib.py import *

so script.py will be:

from tkinter import *
from MyLib.py import *
root = Tk()

frame = Frame(root)
frame.pack()
textDisplay = Entry(frame)
textDisplay.pack()
btn1=Button(root,text='add here ',command=function)
btn1.pack()

root.mainloop()

when i run it show me an error, whats the problem?!

Traceback (most recent call last):
File "c:\Users\Hp\OneDrive\Bureau\emojiDesktopKeyboard\script.py", line 
5, in <module>
from emojiLib import *
File "c:\Users\Hp\OneDrive\Bureau\emojiDesktopKeyboard\emojiLib.py", 
line 1, in <module>
from script import textDisplay
File "c:\Users\Hp\OneDrive\Bureau\emojiDesktopKeyboard\script.py", line 
32, in <module>
btn1=Button(root,text='\U0001F600 ',command=enter1F600 ,width=5,font= 
("Arial", 15), cursor="hand2" , bg="#a9dce3")
NameError: name 'enter1F600' is not defined

>Solution :

One solution that can be used is :

script.py

from tkinter import *
from mylib import myfunc

class Display:
    def __init__(self):
        self.root = Tk()
        self.frame = Frame(root)
        self.frame.pack()
        self.textDisplay = Entry(self.frame)
        self.textDisplay.pack()
        self.btn1=Button(root,text='add here ',command=lambda: myfunc(self.textDisplay))
        self.btn1.pack()

    def show(self):
        self.root.mainloop()

if __name__ == "__main__":
    display = Display()
    display.show()

mylib.py

def function(textDisplay):
    textDisplay.insert(0,'Some String')

Please notice the following points:

  • renamed the library files to fit the usual python standard, and fixed import names
  • removed cyclical import, now we have only one import of mylib in script.py
  • function() has been modified to take an argument into account, that way you can send data from your main script to your module without importing. Then the function is called using a lambda so that it keeps the same signature.
  • created a class Display to try and make it more readable, but it’s not striclty necessary I guess
  • lastly, another way to make all of this work would be to move the implementation of function() to the main script. But I assume you wish to keep it separated.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading