删除最小化和最大化按钮后,是否可以显示toplevel
和root
窗口的图标?我尝试使用-toolwindow
但之后无法显示图标。还有另一种方法可以在显示图标时从窗口中删除最小和最大尺寸按钮吗?
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()
答案 0 :(得分:5)
第一个选项是为toplevel
窗口建立root
窗口transient,这是really simple解决方案。
您需要做的就是将行tp.attributes("-toolwindow", 1)
更改为tp.transient(root)
!
第二个选项更复杂,但在Windows系统中更通用 Windows系统中的每个窗口都有自己的样式,表示为logical disjunction bit-styles。您可以从头开始构建新样式,或者将旧样式设置为旧样式的disjunction,使用位样式(将其打开)或旧样式的conjunction negation位样式(关闭它):
new style = old style AND NOT Maximize AND NOT Minimize
new style = old style OR Maximize OR Minimize
对于所有这些交互,我们需要ctypes库(python附带)和一组WinAPI函数:
检查此示例:
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()
答案 1 :(得分:0)
我刚遇到这个问题,发现 root.resizable(False, False)
之后的 root.geometry
解决了它。