1

I have a python script (we'll say "script.py") that I want to grab values from using a separate GUI python script ("GUI.py"). When I run my GUI.py script, I want to have these text fields in the GUI sent over to script.py after clicking a button in the GUI. I am thinking that my best solution might be to have the GUI.py create another script ("lib.py") that holds all these values and it just writes to it. It then runs the "script.py" script. So all in all the GUI.py will have a function that when called will look something like this:

def on_button(self):
    username = self.usernameInput.get()
    file = open(“lib.py”,”w”) 
    file.write(“username = ” + username) 
    os.system("script.py")

I think this will work, but I am just wondering, does this seem like a practical solution? Let me know what you guys think.

2
  • Nopes it wont work file.write(“username = ” + username) is not the correct way to give input to a Python script, it would instead write these contents directly to your .py file, and can make your .py file corrupted if I enter some bizzare username. Commented May 12, 2017 at 15:32
  • Another option would be to set up your script to read from sys.argv and then pass your arguments through subprocess.call. Commented May 12, 2017 at 16:07

1 Answer 1

3

No, I don't think this is the practical solution.

Do you consider instead making the python script you want to run into a module or package that you can call directly inside your GUI? I think that is the cleanest approach. For using your scripts as modules, see the docs or for 2.7.

Basically a module is a python file, script.py, and as long as it is in the python path (say, your current directory), you can import it:

from script import action

So you could try:

def on_button(self):
    username = self.usernameInput.get()
    result = action(username) # and any other args you want to pass
    print(result)

That is, if the script in question uses a if __name__ == "__main__": statement (or can otherwise be run from the command line), try putting the operations in some def action(args): function and importing it into your GUI.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, it does use an "if name == "main":" statement. However, it is not just username I want to pass in. There are a number of values. I want all of these values passed into script.py and then I want script.py to run. So maybe I should add methods to script.py that allow users to pass in the values? Like "def getUsername(username):"? Or is there a way to run the main function from the GUI?
Absolutely! You can run any function inside the GUI, if you import it and pass the required parameters :). So you can pass more than just the username, or include a config file too if you prefer; point is to call the script as a module within the GUI (no matter how many things you pass to the main function
OK, I think I see what you are saying. If there is a "if name == "main":" function in script.py, then I should try to change this to some sort of "def action(args):" function so that I can pass in all the values. Right? Or wrong?
Yup; you can keep the if name == "main", but copy the logic inside it into a new function that you can import in any other Python program running in the same directory (import it like you would any other import from pip)
if it works (and I hope this is what you're looking for, see the updated answer), please mark the question as answered so others can find it later :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.