0

I have created a custom tkinter window (overrideredirect(1) window) which works quite well. I just had a small issue.

Here's the code:

from tkinter.constants import DISABLED, LEFT, RIGHT
from tkinter.ttk import Button, Label
import pyautogui as pg
import time


root = Tk()
root.geometry("700x700")
root.attributes("-topmost", 1)
root.overrideredirect(1)

ma = False  # For getting window state
tr = 1  # Transparency level
gx = 700 # Geometry X and Y
gy = 700

def nor(event):
    top.config(cursor="arrow")

def place_center():
    # Getting screen size
    reso = pg.size()
    rx = reso[0]    # Resolution: X and Y
    ry = reso[1]
    x = int((rx/2) - (700/2))   # Places to place the window in the center
    y = int((ry/2) - (700/2))
    root.geometry(f"700x700+{x}+{y}")

def drag(event):
    global ma
    root.state("normal")
    top.config(cursor="fleur")  # Changing the cursor
    resb.config(text="     [ ]     ")    # Changing the button
    ma = False  # Window isn't maximized
    fx = root.winfo_pointerx() - 250    # Placing the window as per cursor movement
    fy = root.winfo_pointery() - 10
    root.geometry(f"700x700+{fx}+{fy}")

def restore(event):
    global tr
    tr = 0
    root.overrideredirect(1)
    root.unbind("<Map>")    # So that it wont flicker while restoring
    while tr <= 1.0:    # Fade in animation
        root.attributes("-alpha", tr)
        tr += 0.2
        time.sleep(0.04)
    else:
        root.state('normal')

def minimize():
    global tr
    root.update_idletasks()
    while tr > 0:   # fade out animation
        root.attributes("-alpha", tr)
        tr -= 0.1
    else:
        root.withdraw() # withdrawing the window
        root.overrideredirect(0)    # Activating DWM to be able to minimize
        root.iconify()
    root.bind("<Map>", restore) # To be able to restore the window

def cl():  # Closing the window
    global tr
    if tr > 0:  # Fade out animation
        root.attributes("-alpha", tr)
        tr -= 0.1
        root.after(4, lambda: cl())
    else:
        root.quit()

def zoom(): # To maximize the window
    global ma
    root.state("zoomed")
    ma = True   # Window is maximized
    resb.config(text="     []]     ")    # Changing the maximize button

def maximize():
    global ma
    root.unbind("<Map>")    # To avoid flickering
    if ma == False: # Maximize if not
        zoom()
    else:
        root.state('normal')
        root.geometry(f"700x700")
        ma = False  # Window isn't maximized
        resb.config(text="     [ ]     ")    # Changing the maximize button 

place_center()

# Packing the control buttons and other things (like title bar)
top = Frame(root, height=50, bg='white')
closeb = Button(top, text="      X     ", command=cl)
closeb.pack(side=RIGHT)
resb = Button(top, text="     [ ]     ", command=maximize)
resb.pack(side=RIGHT)
minb = Button(top, text="     __     ", command=minimize)
minb.pack(side=RIGHT)
tit = Label(top, text="My Window", background="white")
tit.pack(side=LEFT)

top.pack(side=TOP, fill=X)

# For the window to move
top.bind("<B1-Motion>", drag)
top.bind("<ButtonRelease-1>", nor)

root.attributes("-topmost", 1)
root.config(bg="grey")

root.mainloop()

The taskbar icon appears only when the window is minimized. I want it to be there even when the window is active.

How can I do that? Thanks!

PS: Sorry if that code isn't understandable! Do let me know.

IJ_123
  • 457
  • 6
  • 13

0 Answers0