在Tkinter中移除最小化/最大化按钮
我有一个用Python写的程序,它会打开一个新窗口来显示一些“关于”的信息。这个窗口有自己的关闭按钮,而且我把它设置成不能调整大小。不过,窗口的最大化和最小化按钮还是存在的,我想把它们去掉。
我使用的是Tkinter,把所有要显示的信息放在Tk类里面。
下面是我目前的代码。我知道代码写得不太好,我打算把信息扩展一下,做成一个类,但我想在继续之前先解决这个问题。
有没有人知道我怎么能控制窗口管理器显示哪些默认按钮呢?
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)
5 个回答
13
Windows
在Windows系统中,你可以使用一个叫做-toolwindow的属性,像这样使用:
root.attributes('-toolwindow', True)
所以如果你想要完整的代码,就是这个:
from tkinter import *
from tkinter import ttk
root = Tk()
root.attributes('-toolwindow', True)
root.mainloop()
还有其他的窗口属性:
-alpha
-transparentcolor
-disabled
-fullscreen
-toolwindow
-topmost
重要提示:这个只在Windows上有效,不适用于MacOS。
Mac
在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。
如果我有错误,请给我留言。
54
一般来说,窗口管理器(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)
这样可以去掉最小化和最大化按钮,但这也取决于窗口管理器。根据我看到的,微软Windows的窗口管理器确实会去掉这些按钮。