self.root.overrideredirect makes the app dissapear from the taskbar. how do i keep it there?
Assistant
In a typical Tkinter application, setting overrideredirect(True) on a window will indeed cause it to both lose its window decorations (such as the title bar and borders) and disappear from the taskbar. This is because overrideredirect(True) tells the window manager to ignore the window, making it independent of the normal window handling mechanisms.
However, if you want to keep the window on the taskbar while also using overrideredirect(True), you will need to implement a workaround. One common approach is to create an invisible root window that will remain on the taskbar and then create a child window with the set.
User
i already have a root, can i separate the new invisible root?
Assistant
If you've already got an existing Tkinter application with a root window and you want to introduce a window that uses overrideredirect(True) while keeping an icon on the taskbar, it can be a bit tricky. Technically, you can't have two Tk instances in the same process without some potential side effects; however, you can create a transient Toplevel window that will not have its own icon on the taskbar but will still be tied to the main root window's taskbar icon.
overrideredirect(True)
Here's a simple example of how you might achieve this with Tkinter:
import tkinter as tk
# Create an invisible root window
root = tk.Tk()
root.geometry('0x0+0+0')
root.overrideredirect(True)
root.attributes('-alpha', 0.0) # Make the root window completely transparent/invisible
# Create a TopLevel window which will be shown instead
app = tk.Toplevel(root)
app.geometry('300x200+100+100') # Set the geometry of the app
app.title('My App')
app.overrideredirect(True) # Remove the window decorations
# Make the app window visible on the taskbar
app.wm_attributes("-alpha", 0.9) # Control the transparency of the window from 0.0 to 1.0
app.wm_attributes("-toolwindow", False) # Avoid making it a tool window
# Add a label to the app window
label = tk.Label(app, text="Welcome to the app!")
label.pack()
# Start the application
root.mainloop()
This code snippet creates an invisible root window and a visible Toplevel window that does not have window decorations and remains on the taskbar. Adjust the window properties to your own needs.
Keep in mind that the behavior of windows can be dependent on the operating system and window manager being used. Some features might not work consistently across different environments. You may need to tweak certain attributes or use platform-specific tricks to get the desired behavior.
Here is a strategy you can use to create a "popup" or "overlay" window that uses overrideredirect(True) without affecting the root window's presence on the taskbar:
import tkinter as tk
def create_overlay():
overlay = tk.Toplevel(root)
overlay.geometry('300x200+100+100') # Desired dimensions and position
overlay.title("Overlay Window")
overlay.overrideredirect(True) # Make the window borderless
# You can add contents to your overlay window below
# e.g., to add a label inside the overlay window
label = tk.Label(overlay, text="This is an overlay")
label.pack()
# This will make the overlay window stay on top of the root window
overlay.transient(root)
return overlay
# Your existing root initialization
root = tk.Tk()
root.geometry('400x300+100+100') # Main window dimensions and position
root.title('Main Application')
# Now you can have buttons or events in your main application that
# triggers the overlay window.
button = tk.Button(root, text='Create Overlay', command=create_overlay)
button.pack()
# Start the application
root.mainloop()
The create_overlay function creates a new Toplevel window, makes it borderless, and ties it as a transient to the root window, so it should still show the main root application in the taskbar. The overlay window won't have its own taskbar entry and will always stay above the root window.
Remember that the behavior of overrideredirect and taskbar icon management can vary between operating systems and window managers. For some platforms, additional platform-specific tweaks might be necessary.