Home > Mobile >  Correct way to run two Tk() mainloops independently, with second being started from first script?
Correct way to run two Tk() mainloops independently, with second being started from first script?

Time:04-18

script_a.py

from tkinter import *
from script_b import test

root = Tk()

var1 = stuff
var2 = stuff

def start_scriptb():
    test([var1, var2])

Button(root, text="Start", 
command=start_scriptb)


root.mainloop()

script_b.py

from tkinter import *


def test(x):
    main = Tk()
    Label(main, text=x)
    Button(main, text="Exit", command=main.destroy())

    main.mainloop() 

This is a very basic version of what I'm trying to achieve. I actually am spawning a progress window that uses subprocess.Popen in script 2 with the passed through variables from script one and viewing the progess through a scrolled text widget on the 2nd program. I'm trying to spawn a new process, independent from the root GUI each time that button is hit from script_a.

It works fine in my tests so far, but I wanted to see if this could cause any issues or if this is actually spawning two processes?

I know there should only be one Tk() per process.

Using a TopLevel() window to show the progress works fine with the threading module, but the TopLevel() window will freeze as well as root (and any other open TopLevels()) if root is doing any sort of processing that takes any length of time.

CodePudding user response:

With many hours of testing, I did have success in running two Tk() loops, but it had potential to be problematic, as "Bryan Oakley" had posted in many threads about.

Ultimately, I decided when I was in need of running something alone, I'd start my GUI with arguments and process it in an entirely new process instead of passing any arguments directly. Seems like a safer option.

  • Related