Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a python script on button click using python and tkinter

I have a python script which has the functionality of sending an email to a user. I executed this script and it is working fine. In another python script I have only a button, so when I click on this button I want the other python script which sends a email to be executed.I have written the following code:

#!/usr/bin/python
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()

def helloCallBack():
    os.system('SendEmail.py')

B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()

I get the following error when I click on the button:

sh: 1:SendEmail.py:not found.

Could you let me know what is the reason for this error and how it can be resolved.Thanks.

like image 912
Valla Avatar asked Nov 10 '14 20:11

Valla


People also ask

How do I run a Python script from Tkinter?

Tkinter ProgrammingImport the Tkinter module. Create the GUI application main window. Add one or more of the above-mentioned widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user.

How do you trigger a Python script?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

How do I run a Python script in GUI?

Use Tkinter to Create a GUI If you're new to creating GUI's in Python the basic steps are to: Import the Tkinter module: import Tkinter, top = Tkinter.Tk() Create the GUI main window that will “house” your GUI and widgets. Add whatever widgets your GUI needs including buttons, labels, lists etc.


3 Answers

I was able to figure out a way to call another python script on button click:

instead of using os.system('SendEmail.py') we need to use os.system('python SendEmail.py')

like image 75
Valla Avatar answered Oct 07 '22 11:10

Valla


import sys
import os
from tkinter import *

window=Tk()

window.title("Running Python Script")
window.geometry('550x200')

def run():
    os.system('opencv_video.py')

btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)

window.mainloop()
like image 5
Raa Avatar answered Oct 07 '22 11:10

Raa


If your SendEmail.py is in the same location, use os.system('SendEmail.py'). If it's in a different location, use os.system('python SendEmail.py').

like image 3
Zzmilanzz Zzmadubashazz Avatar answered Oct 07 '22 12:10

Zzmilanzz Zzmadubashazz