Python Forum
[Tkinter] Open a python program from a button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Open a python program from a button
#1
I have some home-made Python programs which make html.

A simple example is insertMedia.py: it asks for the name of the media file and inserts the audio or video player html code, with the file name inserted, and appends it to my output text file, all in its own <div

This works great in tkinter, thanks to help from here. Enter the name of the file, click a button 'insert audio' or 'insert video and it's done!

What I want to have is a master window. For each "makehtml" program I will make a button. Click the button "insert media" and insertMedia.py should open and run.

I've seen code to open a new window from a window, like this:

import Tkinter as tk

def create_window():
    window = tk.Toplevel(root)

root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()

root.mainloop() 
How can I make insertMedia.py open and run from a master window? What should the command be?
Reply
#2
These things can be tricky,
but have you looked at "import subprocess" ?

Paul
Reply
#3
Here is an example of a toplevel window from an earlier post. Maybe it will help.
Reply
#4
@DPaul: didn't know about subprocess, will look into it, thank you! And yes it was tricky. For me the worst part is converting my "makehtml" functions, which work well in a bash terminal, to work in a tkinter window. It's a whole different kettle of fish! But menator01 put me on the right path, so now I am slowly working through all my functions to make them work in tkinter. I even saw ways to improve them!

@menator01: thanks, I will look at your code later today, see if I can understand it. I never used classes before!

There are 2 simple ways to solve this problem:

1. call the program that opens a new window with os.system

top=Tkinter.Tk()

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

B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()
You just have to make sure the program you want to open is in the system path:

import sys    
    # to import the files we need the paths
    path = '/home/pedro/myPython/myModules/'

    # append the paths
    sys.path.append(path)
2. What I did: put all the programs in a module, in my case guiHTML.py Again, the module must be in the system path, or it won't be found.
Import this module in the master window. Then all the functions are available.

import guiHTML
Put a function like this in the master window:

def openImages():
        #call the images function defined in the guiHTML module
        guiHTML.images()
and link it to a button:

btn3 = tk.Button(frame1, text='open insert image', command=openImages)
    btn3.grid(columnspan=2, column=0, row=2, sticky='w', pady=10)
Now, I don't know what you people who learned programming properly think about this way of doing it and I would be happy to hear suggestions for improvement.

I just know it works!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Creating a restart button for a Pythagorean Theorem Program pav1983 12 1,481 Apr-21-2020, 07:25 AM
Last Post: pav1983
  [PyQt] How to open a program with python without freezing LavaCreeperKing 9 2,170 Aug-17-2019, 08:48 PM
Last Post: LavaCreeperKing
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,291 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [PyQt] Close program using Push Button with the help of input from a Message Box bhargavbn 2 2,539 Oct-30-2018, 05:09 AM
Last Post: bhargavbn
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 8,410 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020