【问题标题】:Removing minimize/maximize buttons in Tkinter删除 Tkinter 中的最小化/最大化按钮
【发布时间】:2011-02-27 12:45:45
【问题描述】:

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

【问题讨论】:

    标签: python windows tkinter


    【解决方案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 确实删除了它们。

    【讨论】:

    • 谢谢。我最终使用了 overrideredirect - 方法并在底部框架中添加了一个脊。看起来还不错。
    • root.resizable(0,0) 在 Ubuntu 中为我工作,我正在使用 tkinter
    • @tzot root.overrideredirect(1) 将完全隐藏外框。将没有关闭、最小或最大选项。如果执行了包含此行的代码,则窗口将永远停留在屏幕上,除非重新启动 IDLE 或关闭操作系统。
    • root.attributes(toolwindow=1) 此命令在 Windows 中不起作用。正确的命令是 root.attributes("-toolwindow",1)。谢谢!
    • 对于任何寻求更灵活的 Windows 解决方案的人来说,this 可能会有用!
    【解决方案2】:
    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 上使用 Python 3,qw.resizable(0,0) on 禁用最大化按钮,但最小化按钮仍然有效。
    【解决方案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
    

    重要提示这仅适用于 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。

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

    【讨论】:

      【解决方案4】:

      使用 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)
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2015-05-02
      • 2019-04-06
      • 2014-08-26
      • 2013-10-10
      • 2011-05-14
      • 1970-01-01
      • 2011-02-09
      • 2019-06-15
      相关资源
      最近更新 更多