1

I've a GUI which will perform some functions when the buttons are pressed.

now i want to create a button in the GUI which will call and run a shell script in the background.

how can i achieve this ?

2

4 Answers 4

3

Not sure if your question is about how to call a shell script in Python, or how to make a button in your GUI. If the former, my comment above (recommending some research on subprocess.Popen) is the solution. Otherwise:

# assuming Python3
import tkinter as tk
import subprocess as sub

WINDOW_SIZE = "600x400"

root = tk.Tk()
root.geometry(WINDOW_SIZE)

tk.Button(root, text="Push me!", command=lambda: sub.call('path/to/script')).pack()
Sign up to request clarification or add additional context in comments.

7 Comments

i know how to create a button i just want to know how to call the shell program, sorry for the ambiguity in the question
@AdamSmith It shows an error saying OSError: [Errno 13] Permission denied
@kingmakerking Then you don't have permission to run that script from where you are. You'll have to get your OS to let you run it -- that's not a Python thing
@AdamSmith No I do have permissions. Still not working?
@kingmakerking That error means the OS is not allowing you to launch that script because you do not have permission to do so. Good luck!
|
1

Python can run shell scripts using the supbprocess module. In order to run it in the background you can start it from a new thread.

To use the module

import subprocess
...
subprocess.call(['./yourScript.sh'])

For a good python threading resource you can try: How to use threading in Python?

Comments

1

Adding to what @lakesh said, below is the complete script :

import Tkinter
import subprocess

top = Tkinter.Tk()

def helloCallBack():
   print "Below is the output from the shell script in terminal"
   subprocess.call('./yourscript.sh', shell=True)


B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()

Please note that the shell script is in the same directory as that of the python script.

If needed, do chmod 777 yourscript.sh

subprocess.call('./yourscript.sh', shell=True)

and import Tkinter and not import tkinter solved the problems I was facing.

Comments

0

Use Tkinter to create a button. For more info, look at this video:http://www.youtube.com/watch?v=Qr60hWFyKHc

Example:

from Tkinter import *

root = Tk()
app = Frame(root)
app.grid()
button1 = Button(app,"Shell Script")
button1.grid()
root.mainloop()

To add the functionality: change button1 line to:

button1 = Button(app,"Shell Script",command=runShellScript)

def runShellScript():
    import subprocess
    subprocess.call(['./yourScript.sh'])

1 Comment

It throws an error " NameError: name 'runShellScript' is not defined

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.