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


Messages In This Thread
Open a python program from a button - by Pedroski55 - Jul-18-2020, 11:55 PM
RE: Open a python program from a button - by DPaul - Jul-20-2020, 03:25 PM
RE: Open a python program from a button - by Pedroski55 - Jul-20-2020, 11:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Creating a restart button for a Pythagorean Theorem Program pav1983 12 7,269 Apr-21-2020, 07:25 AM
Last Post: pav1983
  [PyQt] How to open a program with python without freezing LavaCreeperKing 9 8,217 Aug-17-2019, 08:48 PM
Last Post: LavaCreeperKing
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,012 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 6,679 Oct-30-2018, 05:09 AM
Last Post: bhargavbn

Forum Jump:

User Panel Messages

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