1

I need the next tkinter window to become active automatically after destroying the previous one.

I'm working with Python 3 in Windows 10.

import sys
from tkinter import *

#Green button pressed
def passed():
    fails='0'
    with open("ButtonPressed.txt",'w')as TestResult:
        TestResult.write(fails)
    TestResult.close()
    root.destroy()

#Red button pressed
def failed():
    fails='1'
    with open("ButtonPressed.txt",'w')as TestResult:
        TestResult.write(fails)
    TestResult.close()
    root.destroy()

#First window
def PushHold(UUT):
    global root
    root = Tk()   
    root.bind('<Return>', lambda q:passed())
    TitleText="Push Button test"
    root.title(TitleText)
    root.geometry("460x160")
    frame = Frame(root)
    frame.pack()
    bottomframe = Frame(root)
    bottomframe.pack( side = BOTTOM )

    LABLE_TEXT = "Push and hold S1 on "+ UUT+" board.\n Click OK."
    label = Label( frame, text=LABLE_TEXT)
    label.config(font=("Arial", 22))
    label.pack( side = TOP)

    greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
    greenbutton.config(font=("Arial", 18))
    greenbutton.pack( side = LEFT, padx=140)
    root.mainloop()

#Second window
def Release(UUT):
    global root
    root = Tk()
    root.bind('<Return>', lambda q:passed())
    TitleText="Release Button test"
    root.title(TitleText)
    root.geometry("460x160")
    frame = Frame(root)
    frame.pack()
    bottomframe = Frame(root)
    bottomframe.pack( side = BOTTOM )

    LABLE_TEXT = "Release S1 on "+ UUT+" board.\n Click OK."
    label = Label( frame, text=LABLE_TEXT)
    label.config(font=("Arial", 22))
    label.pack( side = TOP)

    greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
    greenbutton.config(font=("Arial", 18))
    greenbutton.pack( side = LEFT, padx=140)
    root.mainloop()

#read results of the first window
def PushButton(UUT):
    PushHold(UUT)
    with open("ButtonPressed.txt",'r') as TestResult:
        str = list(TestResult)
        fails = int(str[0]) 
    TestResult.close()
    return fails

#read results of the second window
def ReleaseButton(UUT):
    Release(UUT)
    with open("ButtonPressed.txt",'r') as TestResult:
        str = list(TestResult)
        fails = int(str[0]) 
    TestResult.close()
    return fails

PushButton('UUT1')    #first window calling
ReleaseButton('UUT1')    #second window calling

The first window is working good, the second window appears, but not active. It is necessary to click on it to activate before pressing the button.

Trials to use root.withdraw() or root.lift() did not succeed.

mnm
  • 1,962
  • 4
  • 19
  • 46

1 Answers1

0

On my system the second window takes focus without altering your code, so it is hard to tell. You could probably use root.focus_force() prior to calling root.mainloop()

Alternatively, you could spawn different windows from the same root, using tk.Toplevel.

import sys
from tkinter import *

#Green button pressed
def passed():
    fails='0'
    with open("ButtonPressed.txt",'w')as TestResult:
        TestResult.write(fails)
    TestResult.close()
    root.destroy()

#Red button pressed
def failed():
    fails='1'
    with open("ButtonPressed.txt",'w')as TestResult:
        TestResult.write(fails)
    TestResult.close()
    root.destroy()

#First window
def PushHold(UUT):
    global root
    root = Tk()   
    root.bind('<Return>', lambda q:passed())
    TitleText="Push Button test"
    root.title(TitleText)
    root.geometry("460x160")
    frame = Frame(root)
    frame.pack()
    bottomframe = Frame(root)
    bottomframe.pack( side = BOTTOM )

    LABLE_TEXT = "Push and hold S1 on "+ UUT+" board.\n Click OK."
    label = Label( frame, text=LABLE_TEXT)
    label.config(font=("Arial", 22))
    label.pack( side = TOP)

    greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
    greenbutton.config(font=("Arial", 18))
    greenbutton.pack( side = LEFT, padx=140)
    root.focus_force()         #<-------------- Here ---------------------
    root.mainloop()

#Second window
def Release(UUT):
    global root
    root = Tk()
    root.bind('<Return>', lambda q:passed())
    TitleText="Release Button test"
    root.title(TitleText)
    root.geometry("460x160")
    frame = Frame(root)
    frame.pack()
    bottomframe = Frame(root)
    bottomframe.pack( side = BOTTOM )

    LABLE_TEXT = "Release S1 on "+ UUT+" board.\n Click OK."
    label = Label( frame, text=LABLE_TEXT)
    label.config(font=("Arial", 22))
    label.pack( side = TOP)

    greenbutton = Button(frame, text="OK", bg="Green", fg="White", height=2 , width = 10 , command=lambda: passed())
    greenbutton.config(font=("Arial", 18))
    greenbutton.pack( side = LEFT, padx=140)
    root.focus_force()         #<-------------- Here ---------------------
    root.mainloop()

#read results of the first window
def PushButton(UUT):
    PushHold(UUT)
    with open("ButtonPressed.txt",'r') as TestResult:
        str = list(TestResult)
        fails = int(str[0]) 
    TestResult.close()
    return fails

#read results of the second window
def ReleaseButton(UUT):
    Release(UUT)
    with open("ButtonPressed.txt",'r') as TestResult:
        str = list(TestResult)
        fails = int(str[0]) 
    TestResult.close()
    return fails

PushButton('UUT1')    #first window calling
ReleaseButton('UUT1')    #second window calling
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80