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

2024-09-23 22:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)

Tags: textselfevent信息defcontact按钮label
3条回答

窗户

对于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

Important note this is only working with Windows. Not MacOS

苹果

使用mac,您可以使用overredirect属性和“x”按钮关闭窗口,这样就可以完成此任务了:我希望:

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/

对我来说,它正在工作,我有一个Windows7

如果我有错误,请评论我

from tkinter import  *

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

enter image description here

from tkinter import  *

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

enter image description here

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

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

一般来说,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)

这将删除最小/最大按钮,但也取决于窗口管理器。据我所知,微软视窗WM确实删除了它们

相关问题 更多 >