I have an tkinter application with several windows, which often are placed over each other in a window stack, so that only a topmost window is visible. In this topmost window I can press a button which causes one of the not visible windows to run popen. The process which is started is also a tkinter application and is finished automatically some milliseconds later. After this, without any other action by the user, the topmost window is moved behind the second window of my window stack and the second window gets the topmost window.
Why is that? How can I keep the topmost window at top?
With my example code you have to create several windows by the "new window" button. Then move all windows over each other in the order the window number gives. Put the window with number 1 as topmost window. Then press "run in last window" and a new different window will show. Press there the "exit" Button and then the window with number 2 will jump into foreground.
import tkinter as tk
import subprocess
all_windows = []
count = 1
class ButtonWindow(tk.Toplevel):
def __init__(self):
global count
super().__init__()
self.title(count)
count += 1
button_new = tk.Button(self, command=ButtonWindow, text="new window", width=50)
button_run = tk.Button(self, command=self.run_in_last_window, text="run in last window", width=50)
button_new.grid()
button_run.grid()
all_windows.append(self)
def run_in_last_window(self):
last_window = all_windows[-1]
last_window.run_popen()
def run_popen(self):
print("run popen in", self)
command_array = ["Py", "-c", "import tkinter as tk; root=tk.Tk();b=tk.Button(root,text='exit',command=exit);b.grid();root.mainloop()"]
process = subprocess.Popen(command_array, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in process.stdout:
print("line =", line)
process.wait()
root = tk.Tk()
root.withdraw()
ButtonWindow()
root.mainloop()