Python Forum
Now if window is open or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Now if window is open or not
#1
Hello
i try to build a programme with a root windows and other small windows (tk.scale).
In openCv there is the fonction IsOpen() to now if a camera is runing.
i'm looking for the same thing in python to now if a window is started or if he is closed.

thanks for your help. Think
Reply
#2
import tkinter as tk


def check_window():
    if window and tk.Toplevel.winfo_exists(window):
        label["text"] = window.state()
    else:
        label["text"] = "Window does not exist"


def close_window():
    window.withdraw()
    check_window()


def open_window():
    global window

    if window and tk.Toplevel.winfo_exists(window):
        window.deiconify()
    else:
        window = tk.Toplevel(root)
        tk.Label(window, text='This is the child window').pack(padx=50, pady=50)
    check_window()


window = None
root = tk.Tk()
label = tk.Label(root, width=30)
label.pack(padx=20, pady=10)
tk.Button(root, text="Update", command=check_window).pack(padx=10, pady=10)
tk.Button(root, text="Deiconify", command=open_window).pack(padx=10, pady=10)
tk.Button(root, text="Withdraw", command=close_window).pack(padx=10, pady=10)
check_window()
root.mainloop()
You might want to withdraw top level windows instead of destroying them when the window is closed by the window manager. To do this you need to specify a protocol.
import tkinter as tk


def check_window():
    label["text"] = window.state()


def close_window():
    window.withdraw()
    check_window()


def open_window():
    window.deiconify()
    check_window()


window = None
root = tk.Tk()

window = tk.Toplevel(root)
window.withdraw()
tk.Label(window, text='This is the child window').pack(padx=50, pady=50)
window.protocol("WM_DELETE_WINDOW", window.withdraw)  # Withdraw instead of delete

label = tk.Label(root, width=30)
label.pack(padx=20, pady=10)
tk.Button(root, text="Update", command=check_window).pack(padx=10, pady=10)
tk.Button(root, text="Deiconify", command=open_window).pack(padx=10, pady=10)
tk.Button(root, text="Withdraw", command=close_window).pack(padx=10, pady=10)
check_window()
root.mainloop()
Reply
#3
Thanks deanhystad,
your code work with tk.Toplevel,
my problem is that there is no protocol method with tk. Scale. So I use winfo_viewable()
if curssor_expo.winfo_viewable() == True :
        print("Yes, it's wonderful !")
but I am profusely insulted at the closing of the window. Either I manage exeptions, or there is a more appropriate way
Reply
#4
tk.Scale is a widget, not a window. It will be visible if your code created it and placed/packed/grid it somewhere in a window that is visible. You should not need to ask if a widget is visible because there is nothing outside your program that can make it not be visible.

Do you want to ask a widget if it's parent window is visible? You can get the parent window for a widget and use the methods presented in my previous post to determine if the window is open.
window = widget.winfo_toplevel()
Do you want to know if the widget is mapped in it's window? Use winfo_ismapped().
import tkinter as tk


def toggle():
    if scale.winfo_ismapped():
        scale.pack_forget()
    else:
        scale.pack()


root = tk.Tk()
frame = tk.Frame(root)
scale = tk.Scale(frame)
button = tk.Button(root, text="Toggle", command=toggle)
frame.pack()
scale.pack()
button.pack()

root.mainloop()
If none of those are helpful can you provide some code to help me understand your question?
Reply
#5
i ave create that:
bool_curssor_expo = False

def extinction_expo_bool():
    global bool_curssor_expo
    bool_curssor_expo = False

def open_cursseur_expo():
    global bool_curssor_expo
    cursseur_expo.update()
    bool_curssor_expo = True
    cursseur_expo.wait_window(extinction_expo_bool())
    print("bool_curs_expo : ",bool_curssor_expo)
i think is gona work Dance
Reply
#6
Pretty sure that will not work. extinction_expo_bool() is called before you call cursseur_expo.wait_window(). Are you trying to pass a function that is called later? If so, leave off the ().

As I mentioned before, tk.scale is a widget, not a window. You cannot set the close protocol on a widget, but you can't close a widget either. You can set the closing protocol for a window that contains a scale widget. Set the window protocol for the scale widget's window so closing the window calls window.withrdaw() instead of window.close(). Then you don't have to worry about windows and widgets getting deleted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 493 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,511 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,902 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  Subprocess window won't open on Mac blakefindlay 1 2,170 Feb-06-2021, 04:23 AM
Last Post: deanhystad
  Second Window can't open due to iExit function Jionni 4 2,537 Feb-29-2020, 06:06 PM
Last Post: Jionni
  tkinter window and turtle window error 1885 3 6,721 Nov-02-2019, 12:18 PM
Last Post: 1885
  update a variable in parent window after closing its toplevel window gray 5 9,084 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020