Is there any way to share a variable between a library and a main script in python?
Question:
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
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:UsersHpOneDriveBureauemojiDesktopKeyboardscript.py", line
5, in <module>
from emojiLib import *
File "c:UsersHpOneDriveBureauemojiDesktopKeyboardemojiLib.py",
line 1, in <module>
from script import textDisplay
File "c:UsersHpOneDriveBureauemojiDesktopKeyboardscript.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
Answers:
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(self.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.
I try what @SpaceBurger propose, and it works:
script.py
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= lambda: myFunction(textDisplay))
btn1.pack()
root.mainloop()
MyLib.py
def myFunction(textDisplay):
textDisplay.insert(0,'Some String')
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
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:UsersHpOneDriveBureauemojiDesktopKeyboardscript.py", line
5, in <module>
from emojiLib import *
File "c:UsersHpOneDriveBureauemojiDesktopKeyboardemojiLib.py",
line 1, in <module>
from script import textDisplay
File "c:UsersHpOneDriveBureauemojiDesktopKeyboardscript.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
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(self.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
inscript.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.
I try what @SpaceBurger propose, and it works:
script.py
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= lambda: myFunction(textDisplay))
btn1.pack()
root.mainloop()
MyLib.py
def myFunction(textDisplay):
textDisplay.insert(0,'Some String')