How to remove minimize/maximize buttons while preserving the icon?

2024/10/1 17:30:40

Is it possible to display the icon for my toplevel and root window after removing the minimize and maximize buttons? I tried using -toolwindow but the icon can't be displayed afterwards. Is there another way I can remove the min and max size buttons from window while still displaying the icon?

from tkinter import *def top():tp = Toplevel()tp.geometry("300x300")tp.attributes("-toolwindow", 1)tp.iconbitmap("My icon.ico")root = Tk()
root.geometry("400x400")b = Button(root, text="open window with icon", command=top).pack()root.mainloop()
Answer

Windows-specific solution

First option is to make your toplevel window transient for the root window which is a really simple solution.
All you need to do is change line tp.attributes("-toolwindow", 1) to tp.transient(root)!

Second option is more complicated, but more universal within the Windows system.
Each window in the Windows system has it's own style, which is represented as a logical disjunction of bit-styles. You can build the new style from a scratch or set new style to disjunction of the old one with a bit-style (to switch it on) or to conjunction of the old one with a negation of bit-style (to switch it off):

  • To switch off minimize/maximize:
    new style = old style AND NOT Maximize AND NOT Minimize
  • To switch on minimize/maximize:
    new style = old style OR Maximize OR Minimize

For all that interaction we need the ctypes library (comes with python) and a set of the WinAPI functions:

  • GetWindowLong - to get a current style of the window
  • SetWindowLong - to set a new style of the window
  • SetWindowPos - to update the window (note remarks section)
  • GetParent - to get a hwnd of the owner window, because we're trying to make changes in the non-client area.

Check this example:

import tkinter as tk
import ctypes#   shortcuts to the WinAPI functionality
set_window_pos = ctypes.windll.user32.SetWindowPos
set_window_long = ctypes.windll.user32.SetWindowLongPtrW
get_window_long = ctypes.windll.user32.GetWindowLongPtrW
get_parent = ctypes.windll.user32.GetParent#   some of the WinAPI flags
GWL_STYLE = -16WS_MINIMIZEBOX = 131072
WS_MAXIMIZEBOX = 65536SWP_NOZORDER = 4
SWP_NOMOVE = 2
SWP_NOSIZE = 1
SWP_FRAMECHANGED = 32def hide_minimize_maximize(window):hwnd = get_parent(window.winfo_id())#   getting the old styleold_style = get_window_long(hwnd, GWL_STYLE)#   building the new style (old style AND NOT Maximize AND NOT Minimize)new_style = old_style & ~ WS_MAXIMIZEBOX & ~ WS_MINIMIZEBOX#   setting new styleset_window_long(hwnd, GWL_STYLE, new_style)#   updating non-client areaset_window_pos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED)def show_minimize_maximize(window):hwnd = get_parent(window.winfo_id())#   getting the old styleold_style = get_window_long(hwnd, GWL_STYLE)#   building the new style (old style OR Maximize OR Minimize)new_style = old_style | WS_MAXIMIZEBOX | WS_MINIMIZEBOX#   setting new styleset_window_long(hwnd, GWL_STYLE, new_style)#   updating non-client areaset_window_pos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED)def top():tp = tk.Toplevel()#   tp.geometry('300x300')#   tp.iconbitmap('icon.ico')hide_minmax = tk.Button(tp, text='hide minimize/maximize', command=lambda: hide_minimize_maximize(tp))hide_minmax.pack()show_minmax = tk.Button(tp, text='show minimize/maximize', command=lambda: show_minimize_maximize(tp))show_minmax.pack()root = tk.Tk()
root.geometry('400x400')
b = tk.Button(root, text='open window with icon', command=top)
b.pack()root.mainloop()
https://en.xdnf.cn/q/70940.html

Related Q&A

Undefined reference to `PyString_FromString

I have this C code:... [SNIP] ... for(Node = Plugin.Head; Node != NULL; Node = Node->Next) {//Create new python sub-interpreterNode->Interpreter = Py_NewInterpreter();if(Node->Interpreter == N…

How to call a method from a different blueprint in Flask?

I have an application with multiple blueprinted modules.I would like to call a method (a route) that would normally return a view or render a template, from within a different blueprints route.How can …

Dynamically define functions with varying signature

What I want to accomplish:dct = {foo:0, bar:1, baz:2} def func(**dct):pass #function signature is now func(foo=0, bar=1, baz=2)However, the ** syntax is obviously clashing here between expanding a dict…

python pandas plot with uneven timeseries index (with count evenly distributed)

My dataframe has uneven time index.how could I find a way to plot the data, and local the index automatically? I searched here, and I know I can plot something like e.plot()but the time index (x axis)…

python OpenAI gym monitor creates json files in the recording directory

I am implementing value iteration on the gym CartPole-v0 environment and would like to record the video of the agents actions in a video file. I have been trying to implement this using the Monitor wra…

Install xgboost under python with 32-bit msys failing

Trying to install xgboost is failing..? The version is Anaconda 2.1.0 (64-bit) on Windows & enterprise. How do I proceed? I have been using R it seems its quite easy to install new package in R …

Pythons _winapi module

I was trying to write some python code that requires calls to native WINAPI functions. At first I came across the pypiwin32 package. Then, somewhere on the internet I saw someone using the _winapi modu…

list comprehension with numpy arrays - bad practice?

I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach. Here is the code in question:a = np.array([[1,2,3],[4,5,6…

pandas: write dataframe to excel file *object* (not file)?

I have a dataframe that I want to convert to excel file, and return it using HTTP. Dataframes to_excel method accepts either a path, or an ExcelWriter, which, in turn, refers to a path.Is there any way…

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?win32api.SetCursorPos((x,y))vs:nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemM…