销毁前一个窗口后如何激活tkinter窗口?

时间:2019-06-16 10:28:50

标签: python-3.x tkinter

我需要下一个tkinter窗口在销毁前一个窗口后自动激活。

我正在Python 3中使用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

第一个窗口运行良好,第二个窗口出现,但未激活。按下按钮之前,必须先单击它才能激活。

使用root.withdraw()root.lift()的尝试失败。

1 个答案:

答案 0 :(得分:0)

在我的系统上,第二个窗口将在不更改代码的情况下聚焦,因此很难分辨。您可能在致电root.focus_force()

之前先使用root.mainloop()

或者,您可以使用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