22

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.

5
  • If you go to a command prompt and type SendEmail.py, what happens? Do you get the same error? Commented Nov 10, 2014 at 20:44
  • no it works fine.I executed the same python file separately and then it worked fine.I used python SendEmail.py and it worked. Commented Nov 10, 2014 at 20:53
  • 4
    Do you notice what you just wrote? You said "I used python SendEmail.py". That's not what I asked, and that's not what you're doing in the script. At a prompt, type literally SendMail.py not python SendMail.py. I suspect you'll get the same error. If it doesn't work from the command line, it's not going to work from os.system(). Commented Nov 10, 2014 at 20:55
  • This should be a duplicate of e.g. What is the best way to call a script from another script? but I am out of close votes for today. The question does not actually have anything to do with Tkinter. Commented Sep 4, 2022 at 5:41
  • Does this answer your question? How to call a script from another script? Commented Jun 9, 2024 at 7:48

6 Answers 6

16

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')

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

Comments

6
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()

1 Comment

This doesn't functionally do anything different from the code in the question, it just adds some bells and whistles to the application window.
3

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').

1 Comment

The problem is not the location of the script. The problem is that the script is not recognized as executable by the operating system.
0
#!/usr/bin/python
import sys
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()

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

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

use the keyword "python" to run the command

Comments

0

As an amateur, I am not really qualified to give advice. This is how I did it.

I want to do this kind of thing too. I have about 16 little python programs which make html, sets of checkboxes, sets of radiobuttons, text input fields, html tables etc.

In another thread here a comment was quite deprecative of using os.system calls. Not sure why, but I thought I would try another approach.

I've just started learning tkinter, so I am making each of my 'makehtml' functions run in a window.

Now I want a master window with buttons. Click a button and another window opens, say the checkboxes window, or any of the other windows for making html.

I made a module: guiHTML.py All my 'makehtml' functions are in there.

Import guiHTML in the master window.

import os, sys    
# to import the files we need the paths
path = '/home/pedro/myPython/myModules/'    
# append the paths
sys.path.append(path)

import tkinter as tk
from functools import partial      
import guiHTML

Then, in the master window make a function like this for each button:

def openCheckboxes():
        #call the checkboxes function defined in the guiHTML module
        guiHTML.checkboxes()

Then, in the checkboxes button just put this:

btn3 = tk.Button(frame1, text='insert checkboxes', command=openCheckboxes)
btn3.grid(columnspan=2, column=0, row=2, sticky='w', pady=10)

Click btn3 and the checkboxes window opens.

This works for me, but I don't know if it is a good way to do this. I only began with tkinter a month ago.

If there is a better way to do this, I'd be glad to hear it from you experts!

Comments

-1
#!/usr/bin/python
import sys
import os
import tkinter as tk

root = tk.Tk()

def helloCallBack():
    os.system('call.py')
    #Keep_both_files_in_the_same_Folder
    b1=tk.Button(root, text="Calendar",bg="white",command=helloCallBack)
    b1.pack()
    root.mainloop()

1 Comment

Don't forget to actually answer the question. See stackoverflow.com/help/how-to-answer for some guidelines. The original question was asking for the cause of an error.

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.