如何在保留图标的同时删除最小化/最大化按钮? [英] How to remove minimize/maximize buttons while preserving the icon?

查看:216
本文介绍了如何在保留图标的同时删除最小化/最大化按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在移除最小化和最大化按钮之后,是否可以在我的toplevelroot窗口中显示图标?我尝试使用-toolwindow,但此后无法显示该图标.还有另一种方法可以在仍然显示图标的同时从窗口中删除最小"和最大"按钮吗?

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()

推荐答案

Windows专用解决方案

第一种选择是使您的toplevel窗口瞬变 root窗口,这是一个非常简单的解决方案.
您需要做的就是将tp.attributes("-toolwindow", 1)行更改为tp.transient(root)

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)!

第二个选项更复杂,但在Windows系统中更为通用.
Windows系统中的每个窗口都有其自己的样式,该样式表示为逻辑分离 href ="https://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85).aspx" rel ="noreferrer">位样式.您可以从头开始构建新样式,也可以将新样式设置为旧样式的 disjunction 位样式(将其打开)或使用旧的连接 ="https://en.wikipedia.org/wiki/Negation" rel ="noreferrer">否定位(将其关闭):

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
  • 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

对于所有这些交互,我们需要 ctypes 库(Python附带)以及一组WinAPI函数:

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.

检查此示例:

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()

这篇关于如何在保留图标的同时删除最小化/最大化按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆