简体   繁体   中英

Running another python script using tkinter

I've created a GUI using tkinter, I want another python script to run when I open the GUI.This is the sample code for what I've done so far.

window = Tk()
window.configure(bg='#101d25')
window.maxsize(width=580, height=450)
window.minsize(width=580, height=450)

title = Label(window, text='Face clustering', bg='#232d36', fg='#979ca0', font=('Ink Free', 30, 
'bold'))
title.pack(side=TOP, fill=X)

process_label = Label(window, text='Processing files', fg='#979ca0', bg='#101d25', font=('Ink Free', 
14, 'bold'))
process_label.place(x=70, y=150)

style = ttk.Style()
style.theme_use('clam')
TROUGH_COLOR = '#101d25'
BAR_COLOR = '#979ca0'
style.configure("red.Horizontal.TProgressbar", troughcolor=TROUGH_COLOR, bordercolor=TROUGH_COLOR,
            background=BAR_COLOR, lightcolor=BAR_COLOR, darkcolor=BAR_COLOR)
progress_bar = ttk.Progressbar(window, style="red.Horizontal.TProgressbar", orient=HORIZONTAL, 
length=300,mode="determinate")
progress_bar.place(x=50, y=200)
progress_bar.start()
os.system('python sample.py')
progress_bar.stop()
window.mainloop()

I want the progress bar to be running till the sample.py completes it execution.The file is getting executed but GUI is not displayed. Hoping for a solution.

Thanks in advance

You need to run the script in another thread:

import threading

...

progress_bar.place(x=50, y=200)

def run_script():
    progress_bar.start()
    os.system('python sample.py')
    progress_bar.stop()

threading.Thread(target=run_script, daemon=True).start()

window.mainloop()

If the main code block is inside a function in the sample.py , eg main() , then it is better to import the function and call the function directly:

import threading
from sample import main

...

def run_it():
    progress_bar.start()
    main()
    progress_bar.stop()

threading.Thread(target=run_it, daemon=True).start()

I dont know exactly if os.system can notify you when your script is done or not, but maybe you could try and use the after method. I will attach an example from a GUI i wrote some time ago.

def checkForComplete(self):
    print(self.createStatus)
    if self.createStatus == "notStarted":
        self.createStatus = "started"
        self.progressbar.configure(style = "green.Horizontal.TProgressbar")
        self.progressbar.start()
        self.after(100, self.checkForComplete)
    elif self.createStatus == "succes":
        self.progressbar.stop()
        self.createStatus = "notStarted"
        self.progressbar['value'] = 200
        self.progressbar.update_idletasks()
    elif self.createStatus == "fail":
        self.progressbar.stop()
        self.createStatus = "notStarted"
        self.progressbar.configure(style = "red.Horizontal.TProgressbar")
        self.progressbar['value'] = 200
        self.progressbar.update_idletasks()
    else: #its started, that means we are waiting for the action to complete
        self.after(100, self.checkForComplete)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM