23

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.

Valla
  • 2,414
  • 11
  • 42
  • 73
  • If you go to a command prompt and type `SendEmail.py`, what happens? Do you get the same error? – Bryan Oakley Nov 10 '14 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. – Valla Nov 10 '14 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()`. – Bryan Oakley Nov 10 '14 at 20:55
  • This should be a duplicate of e.g. [What is the best way to call a script from another script?](/questions/1186789/) but I am out of close votes for today. The question does not actually have anything to do with `Tkinter`. – Karl Knechtel Sep 04 '22 at 05:41

6 Answers6

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

Valla
  • 2,414
  • 11
  • 42
  • 73
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()
Raa
  • 61
  • 1
  • 1
  • This doesn't functionally do anything different from the code in the question, it just adds some bells and whistles to the application window. – Karl Knechtel Sep 04 '22 at 05:42
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').

Fabian Schultz
  • 18,138
  • 5
  • 49
  • 56
  • The problem is not the location of the script. The problem is that the script is not recognized as executable by the operating system. – Karl Knechtel Sep 04 '22 at 05:43
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

Dr Sheldon
  • 176
  • 1
  • 14
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!

Pedroski
  • 433
  • 1
  • 7
  • 16
-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()
Michael
  • 3,093
  • 7
  • 39
  • 83
  • Don't forget to actually answer the question. See https://stackoverflow.com/help/how-to-answer for some guidelines. The original question was asking for the cause of an error. – Brian Minton Oct 01 '18 at 17:25