繁体   English   中英

删除 Tkinter 中的最小化/最大化按钮

[英]Removing minimize/maximize buttons in Tkinter

我有一个 python 程序,它打开一个新窗口来显示一些“关于”信息。 这个窗口有它自己的关闭按钮,我把它设为不可调整大小。 但是,最大化和最小化它的按钮仍然存在,我希望它们消失。

我正在使用 Tkinter,包装所有信息以显示在 Tk 类中。

到目前为止的代码如下。 我知道它不漂亮,我计划将信息扩展到一个类,但我想在继续之前解决这个问题。

任何人都知道我可以如何控制 Windows 管理器显示哪些默认按钮?

def showAbout(self):


    if self.aboutOpen==0:
        self.about=Tk()
        self.about.title("About "+ self.programName)

        Label(self.about,text="%s: Version 1.0" % self.programName ,foreground='blue').pack()
        Label(self.about,text="By Vidar").pack()
        self.contact=Label(self.about,text="Contact: adress@gmail.com",font=("Helvetica", 10))
        self.contact.pack()
        self.closeButton=Button(self.about, text="Close", command = lambda: self.showAbout())
        self.closeButton.pack()
        self.about.geometry("%dx%d+%d+%d" % (175,\
                                        95,\
                                        self.myParent.winfo_rootx()+self.myParent.winfo_width()/2-75,\
                                        self.myParent.winfo_rooty()+self.myParent.winfo_height()/2-35))

        self.about.resizable(0,0)
        self.aboutOpen=1
        self.about.protocol("WM_DELETE_WINDOW", lambda: self.showAbout())
        self.closeButton.focus_force()


        self.contact.bind('<Leave>', self.contactMouseOver)
        self.contact.bind('<Enter>', self.contactMouseOver)
        self.contact.bind('<Button-1>', self.mailAuthor)
    else:
        self.about.destroy()
        self.aboutOpen=0

def contactMouseOver(self,event):

    if event.type==str(7):
        self.contact.config(font=("Helvetica", 10, 'underline'))
    elif event.type==str(8):
        self.contact.config(font=("Helvetica", 10))

def mailAuthor(self,event):
    import webbrowser
    webbrowser.open('mailto:adress@gmail.com',new=1)

一般来说,WM(窗口管理器)决定显示什么装饰不能轻易地由像 Tkinter 这样的工具包决定。 所以让我总结一下我所知道的以及我发现的:

import Tkinter as tk

root= tk.Tk()

root.title("wm min/max")

# this removes the maximize button
root.resizable(0,0)

# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)

# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen

root.mainloop()

还有一种可能是,对于根窗口以外的Toplevel窗口,您可以执行以下操作:

toplevel.transient(1)

这将删除最小/最大按钮,但这也取决于窗口管理器。 根据我的阅读,MS Windows WM 确实删除了它们。

from tkinter import  *

qw=Tk()
qw.resizable(0,0)      #will disable max/min tab of window
qw.mainloop()

在此处输入图像描述

from tkinter import  *

qw=Tk()
qw.overrideredirect(1) # will remove the top badge of window
qw.mainloop()

在此处输入图像描述

这是在 tkinter 中禁用最大化和最小化选项的两种方法

请记住图像中显示的按钮代码不在示例中,因为这是有关如何使最大/最小选项卡不起作用或如何删除的解决方案

视窗

对于 Windows,您可以像这样使用 -toolwindow 属性:

root.attributes('-toolwindow', True)

所以如果你想要完整的代码,就是这样

from tkinter import *

from tkinter import ttk

root = Tk()

root.attributes('-toolwindow', True)

root.mainloop()

其他 window.attributes 属性:

-alpha
-transparentcolor
-disabled
-fullscreen
-toolwindow
-topmost

重要说明这仅适用于 Windows。 不是 MacOS

苹果电脑

使用 mac,您可以使用 overredirect 属性和“x”按钮来关闭窗口,这将完成这项工作。 :D 像这样:

from tkinter import *

from tkinter import ttk

window = Tk()

window.overredirect(True)

Button(window, text="x", command=window.destroy).pack()

window.mainloop()

灵感来自https://www.delftstack.com/howto/python-tkinter/how-to-create-full-screen-window-in-tkinter/

对我来说,它正在工作,我有一个 Windows 7。

如果我有错误,请评论我。

使用 ctypes 删除最小化/最大化按钮

    import ctypes as ct

    set_window_pos = ct.windll.user32.SetWindowPos
    set_window_long = ct.windll.user32.SetWindowLongPtrW
    get_window_long = ct.windll.user32.GetWindowLongPtrW
    get_parent = ct.windll.user32.GetParent

    # Identifiers
    gwl_style = -16

    ws_minimizebox = 131072
    ws_maximizebox = 65536

    swp_nozorder = 4
    swp_nomove = 2
    swp_nosize = 1
    swp_framechanged = 32

    hwnd = get_parent(settings_panel.winfo_id())
    # Get the style
    old_style = get_window_long(hwnd, gwl_style)
    # New style, without max/min buttons
    new_style = old_style & ~ ws_maximizebox & ~ ws_minimizebox
    # Apply the new style
    set_window_long(hwnd, gwl_style, new_style)
    # Updates
    set_window_pos(hwnd, 0, 0, 0, 0, 0, swp_nomove | swp_nosize | swp_nozorder | swp_framechanged)

我合并了@demyaN 和其他人的答案,以下是完成工作的一种方法。

import ctypes as ct 
from tkinter import *

def setWinStyle(root):
    set_window_pos = ct.windll.user32.SetWindowPos
    set_window_long = ct.windll.user32.SetWindowLongPtrW
    get_window_long = ct.windll.user32.GetWindowLongPtrW
    get_parent = ct.windll.user32.GetParent

    # Identifiers
    gwl_style = -16

    ws_minimizebox = 131072
    ws_maximizebox = 65536

    swp_nozorder = 4
    swp_nomove = 2
    swp_nosize = 1
    swp_framechanged = 32

    hwnd = get_parent(root.winfo_id())

    old_style = get_window_long(hwnd, gwl_style) # Get the style

    new_style = old_style & ~ ws_maximizebox & ~ ws_minimizebox # New style, without max/min buttons

    set_window_long(hwnd, gwl_style, new_style) # Apply the new style

    set_window_pos(hwnd, 0, 0, 0, 0, 0, swp_nomove | swp_nosize | swp_nozorder | swp_framechanged)     # Updates

window = Tk()
Button(window, text="button").pack() # add your widgets here.
window.after(10, lambda: setWinStyle(window)) #call to change style after the mainloop started. Directly call setWinStyle will not work.
window.mainloop()

顺便说一句,使用window.attributes('-toolwindow', True)将删除最小化和最大化框,但它会使应用程序不显示在任务栏中,这对我来说是个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM