0

I'm doing a basic GUI to provide some user feedback after some shell commands, a little interface for a shell script really.

Showing a TK window, waiting for a os.system call to complete and updating the TK window multiple times, after each os.system call.

How does threading work with tk?

That's it, thanks!

Luke Stanley
  • 1,274
  • 1
  • 16
  • 32

2 Answers2

1

The standard threading library should be perfectly okay if you run it with Tk. This source says, that you should just let the main thread run the gui and create threads for your os.system() calls.

You could write an abstraction like this which updates your GUI upon finishing the task:

def worker_thread(gui, task):
    if os.system(str(task)) != 0:
        raise Exception("something went wrong")
    gui.update("some info")

The thread can be started using thread.start_new_thread(function, args[, kwargs]) from the standard library. See the documentation here.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

Just a basic example of what I did, with credit to Constantinius for pointing out that Thread works with Tk!

import sys, thread
from Tkinter import *
from os import system as run
from time import sleep

r = Tk()
r.title('Remote Support')
t = StringVar()
t.set('Completing Remote Support Initalisation         ')
l = Label(r,  textvariable=t).pack() 
def quit():
    #do cleanup if any
    r.destroy()
but = Button(r, text='Stop Remote Support', command=quit)
but.pack(side=LEFT)

def d():
    sleep(2)
    t.set('Completing Remote Support Initalisation, downloading, please wait         ')
    run('sleep 5') #test shell command
    t.set('Preparing to run download, please wait         ')
    run('sleep 5')
    t.set("OK thanks! Remote Support will now close         ")
    sleep(2)
    quit()

sleep(2)
thread.start_new_thread(d,())
r.mainloop()
Luke Stanley
  • 1,274
  • 1
  • 16
  • 32