Search code examples
pythonpython-3.xpython-requestscoding-stylepython-imaging-library

How can I design a GUI run button for my Python script? The button which only responsible for click to run the script


I have prepared a python automation script, I want to design a GUI button which should have the only function to run the script instead of run a command like below in terminal.

python3 media/path/xyz.py

Solution

  • You can either use os.startfile() or subprocess.call()

    Here is a small example for your reference

    from tkinter import *
    import os
    #import subprocess
    root=Tk()
    def run_p():
        os.startfile('media//path//xyz.py')
        #Or
        #subprocess.call('C:\\myprogram.py')
    btn=Button(root,text='Click',command=run_p).pack()
    root.mainloop()