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 ?

ayaan
  • 715
  • 5
  • 18
  • 36

4 Answers4

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()
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 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 – ayaan Jul 30 '14 at 04:42
  • @AdamSmith It shows an error saying OSError: [Errno 13] Permission denied – kingmakerking Oct 29 '14 at 16:01
  • @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 – Adam Smith Oct 29 '14 at 16:15
  • @AdamSmith No I do have permissions. Still not working? – kingmakerking Oct 29 '14 at 17:06
  • @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! – Adam Smith Oct 29 '14 at 17:07
  • @AdamSmith I set the permission to 777 and executed as python pythoncode.py and it didnt do anything. Didnt open any window, neither any message. – kingmakerking Oct 29 '14 at 19:33
  • @kingmakerking sorry this seems like a completely different issue than the one my answer addresses. Maybe consider writing your own question with the code in question? – Adam Smith Oct 29 '14 at 19:54
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?

Community
  • 1
  • 1
J.Miller
  • 477
  • 1
  • 3
  • 14
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.

kingmakerking
  • 2,017
  • 2
  • 28
  • 44
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'])
lakshmen
  • 28,346
  • 66
  • 178
  • 276