0

i have a python 3 script('script1') on windows, and i try to pass a variable('var') to another script('script2').

script1:

import subprocess
from tkinter import *
w=Tk()
def script2(var):
    subprocess.Popen('script2.py',stdout=subprocess.PIPE,shell=True)
b1=Button(w,text='var 1',command=lambda:script2('var 1'))
b1.pack()
b2=Button(w,text='var 2',command=lambda:script2('var 2'))
b2.pack()
w.mainloop()

script2:

print(var)

nothing happens...thx all

Tommy
  • 101
  • 8
  • When using a subprocess, this is only possible by using command line arguments, would that be an option for you? – FlyingTeller Mar 22 '22 at 09:54
  • do see the UI with buttons? – Numan Ijaz Mar 22 '22 at 09:56
  • What is your platform? `subprocess.Popen('script2.py',...)` does not work in Windows. It should be `subprocess.Popen([sys.executable, 'script2.py'], ...)`. However using `subprocess` to run a Python script, the script will be executed as a new process and so it cannot access any variable in current process. You need to pass it using command line argument and use `sys.argv` to access it inside `script2.py`. – acw1668 Mar 22 '22 at 10:06
  • @NumanIjaz yes i do – Tommy Mar 22 '22 at 11:13
  • @acw1668 have u got a doc link please? – Tommy Mar 22 '22 at 11:13
  • 1
    It is easy to search on internet. For example, search `python sys.argv` and you will find examples on using `sys.argv`. – acw1668 Mar 22 '22 at 11:24

0 Answers0