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()
Use the ResizeMode property By setting the ResizeMode property to CanMinimize only the Minimize button will be visible. The maximize button will be hidden and the resizing will be disabled. This is demonstrated in Example 2.
Alternative Method to Hiding/Showing the MenuRight-click over the titles of the menu and select the Minimize the Ribbon option. To make it full-size again, right-click over the titles of the menu and select the Minimize the Ribbon option again.
Try this code: JFrame frame = new JFrame("Example"); frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.
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):
new style = old style AND NOT Maximize AND NOT Minimize
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:
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 = -16
WS_MINIMIZEBOX = 131072
WS_MAXIMIZEBOX = 65536
SWP_NOZORDER = 4
SWP_NOMOVE = 2
SWP_NOSIZE = 1
SWP_FRAMECHANGED = 32
def hide_minimize_maximize(window):
hwnd = get_parent(window.winfo_id())
# getting the old style
old_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 style
set_window_long(hwnd, GWL_STYLE, new_style)
# updating non-client area
set_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 style
old_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 style
set_window_long(hwnd, GWL_STYLE, new_style)
# updating non-client area
set_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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With