[英]How to remove minimize/maximize buttons while preserving the icon?
Is it possible to display the icon for my toplevel
and root
window after removing the minimize and maximize buttons?删除最小化和最大化按钮后,是否可以显示
toplevel
和root
窗口的图标? I tried using -toolwindow
but the icon can't be displayed afterwards.我尝试使用
-toolwindow
但之后无法显示图标。 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()
First option is to make your toplevel
window transient for the root
window which is a really simple solution.第一个选项是使您的
toplevel
窗口为root
窗口瞬态,这是一个非常简单的解决方案。
All you need to do is change line tp.attributes("-toolwindow", 1)
to tp.transient(root)
!您需要做的就是将
tp.attributes("-toolwindow", 1)
行更改为tp.transient(root)
!
Second option is more complicated, but more universal within the Windows system.第二种选择更复杂,但在 Windows 系统中更通用。
Each window in the Windows system has it's own style, which is represented as a logical disjunction of bit-styles . Windows 系统中的每个窗口都有自己的样式,它表示为位样式的逻辑分离。 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:对于所有这些交互,我们需要ctypes库(python 附带)和一组 WinAPI 函数:
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()
我刚遇到这个问题,发现root.resizable(False, False)
在root.geometry
解决了它之后。
root.protocol("WM_DELETE_WINDOW", False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.