1

I want to prevent my tkinter window from minimizing but able to maximise and close the window.

Something like this

enter image description here

How do I do this?

  • 1
    [If you intrested in windows styles](https://stackoverflow.com/questions/69603075/tkinter-customization-on-high-level/69605702#69605702) – Thingamabobs Nov 22 '21 at 09:19

1 Answers1

3

If your platform is Windows (looks like it is based on the image), you can call Windows functions via pywin32 module to achieve the goal:

import tkinter as tk
import win32gui
import win32con

root = tk.Tk()
root.title('MinimizeTest')
root.geometry('300x200')

def disable_minbox():
    # get window handle
    win_id = root.winfo_id()
    hwnd = win32gui.GetParent(win_id) 
    # get the current window style of root window
    style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
    # mask out minimize button
    style &= ~win32con.WS_MINIMIZEBOX
    # update the window style of root window
    win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, style)

# need to do it when root window is displayed
root.after(1, disable_minbox)
root.mainloop()

Note that you need to install pywin32 using pip:

pip install pywin32
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Thank you so much! It works! Do you mind if you briefly explain the code? Thanks. EDIT: like what does the win32con.GWL_STYLE do –  Nov 22 '21 at 07:59
  • @CDN Update answer with explanation on each step. – acw1668 Nov 22 '21 at 08:03
  • @CDN Detail information on [`SetWindowLong()`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlonga) and [window styles](https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles). – acw1668 Nov 22 '21 at 08:09