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

6
  • When using a subprocess, this is only possible by using command line arguments, would that be an option for you? Commented Mar 22, 2022 at 9:54
  • do see the UI with buttons? Commented Mar 22, 2022 at 9: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. Commented Mar 22, 2022 at 10:06
  • @NumanIjaz yes i do Commented Mar 22, 2022 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. Commented Mar 22, 2022 at 11:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.