Valla
Valla

Reputation: 2442

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.

Upvotes: 22

Views: 71619

Answers (6)

Pedroski
Pedroski

Reputation: 497

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!

Upvotes: 0

Dr Sheldon
Dr Sheldon

Reputation: 186

#!/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

Upvotes: 0

Raa
Raa

Reputation: 61

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

Upvotes: 6

mohit mali
mohit mali

Reputation: 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()

Upvotes: -1

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

Upvotes: 3

Valla
Valla

Reputation: 2442

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

Upvotes: 16

Related Questions