Pass Variable Between Python Scripts

pass variable between main functions along different python scripts

if __name__ == "__main__": is not a function. It is a check whether the script is run as the main script and not imported. So when you import one.py, everything inside its if __name__ == "__main__": block is ignored.

Get your variable le out from this if statement and from one import le should work.

Pass a variable between two scripts?

For one thing, script2.information() is a generator function which means you will have to manually iterate the generator object returned on the first call it to get successive values.

For another, tkinter doesn't support multithreading. One thing you can do is schedule for a function to be called after a certain amount of time using the universal widget method after(). In this case, it can be used to schedule a call to an (added) function that iterates the generator object after it's been created by calling script2.information() and updates the StringVar() widget accordingly each time it's called.

You also need to change script2.py so it doesn't call time.sleep(). Doing so will make your tkinter GUI program hang whenever it's called (since it temporarily interrupts execution of the tkinter mainloop()).

Modified script2.py:

import time

def information():
variable = 0
while variable < 60: # changed from 500 for testing
yield variable
variable += 1
print(variable)
# time.sleep(1) # don't call in tkinter programs

main script:

from tkinter import *
import script2

DELAY = 100 # in millisecs

root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()

def do_update(gen, var):
try:
next_value = next(gen)
except StopIteration:
var.set('Done!')
else:
var.set(next_value)
root.after(DELAY, do_update, gen, var) # call again after delay

def run_script(var):
gen = script2.information() # create generator object
do_update(gen, var) # start iterating generator and updating var

var = StringVar()
var.set('Waiting for Input...')

button1 = Button(root, text="Run script!", command=lambda: run_script(var))
button1.pack()

status = Label(root, textvariable=var, bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

root.mainloop()

Pass variables to python script included using exec(open().read())

I tried this code you are mentioned is working I think you are running action.py technically you need to run the main.py file that contained this code.

age = 32
def printName(name):
print(name)
exec(open("action.py").read())

Sample Image

passing variable value from django to another python script

Generally, according to django you should include the python code in the view itself if possible. Django views helps to deal with models, reverse or redirect or call urls and renders the templates.

We can't run python seperately in Django application. In order to do that, there are some ways to do it. One of those easiest ways to do it is writing a basic Django command and call it from the django view. In this way we can send the parameters to the django command from the view.



Related Topics



Leave a reply



Submit