Home > Blockchain >  How to make tkinter window pop-up when a certain if statement is triggered?
How to make tkinter window pop-up when a certain if statement is triggered?

Time:09-17

Hey guys so I am trying to make a timer app for studying which pops-up when break time is triggered. I am using this code to bring the window up, but the issue I am facing is if I minimize the window this code does not work anymore and the app does not pop-up. I tried to find a way to just remove the minimize button option from the window but didn't come across anything.

def raise_above_all(window):
    window.attributes('-topmost', 1)
    window.attributes('-topmost', 0)

CodePudding user response:

Don't go for disabling the window's minimize button. Just use window.state('zoomed') along with window.attributes('-topmost', True). This way even if the window was minimized, still it will be raised to the topmost level. Also, define the window's maxsize prior, so that the window does not become fullscreen due to zoomed.

Here is a simple yet complete demonstration: (To raise the window "window2", you would need to Enter 1 in the entry box of "window1")

from tkinter import *


window1 = Tk()
window1.geometry('1200x600')
window1.minsize(1200, 600)

def raise_above_all(window):
    window.state('zoomed')  #Needed in case the window2 is minimized
    window.attributes('-topmost', True)

def create_new_window2(window):
    window.maxsize(300, 300)    #define the maxsize so that the window2 does not get fullscreen when 'zoomed'
    window.mainloop()

def submit():
    if x.get()==1:
        raise_above_all(window2)
    else:
        print("Enter 1")
        
x = IntVar()
Entry(window1, textvariable=x).pack()
Button(window1, text="Enter", command=submit).pack()

window2 = Tk()
create_new_window2(window2)

window1.mainloop()

Alternatively, you can just remove the entire window's title bar using window.overrideredirect(True)

CodePudding user response:

You can use window.deiconify() to make it un-minimize, and use your code to make it go to front. Like this:

window.deiconify()
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
  • Related