【问题标题】:Python 3 - tkinter adding a minimize buttonPython 3 - tkinter 添加最小化按钮
【发布时间】:2018-12-03 15:40:27
【问题描述】:



我想在 ### 中添加一个自定义最小化按钮,但我真的不知道怎么做。
我试过找了一下,但只找到了 Python 2 个不再适用于 Python 3 的结果。

如果 linux 和 windows 都可以(在开始时进行操作系统检查,我可以自己进行)并且不使用导入的模块,那将是最好的关闭基本的 Python 模块。

这是我当前的代码:

from tkinter import *
import ctypes

root = Tk()
usr32 = ctypes.windll.user32
res1 = usr32.GetSystemMetrics(0)
res2 = usr32.GetSystemMetrics(1)

#-------------------------------

def ext():
    exit()


def minim():
    ###


root.resizable(0, 0)
root.geometry(str(int(res1 * 0.15)) + "x" + str(int(res2 * 0.1)))
root.overrideredirect(1)

back = Frame(root, bg='black')
back.pack_propagate(0)
back.pack(fill=BOTH, expand=1)

b_Frame = Frame(back, bg="#505050")
b_Frame.place(x=0, y=0, anchor="nw", width=res1 * 0.15, height=res2 * 0.025)

Ext_but = Button(b_Frame, text="X", bg="#FF6666", fg="white", command=ext)
Ext_but.place(x=res1 - res1 * 0.8665, y=0, anchor="nw", width=res1 * 0.016, height=res2 * 0.025)

Min_but = Button(b_Frame, text="_", bg="#FF6666", fg="white", command=minim)
Min_but.place(x=res1 - res1 * 0.8825, y=0, anchor="nw", width=res1 * 0.016, height=res2 * 0.025)

#-------------------------------

root.mainloop()

【问题讨论】:

    标签: python-3.x windows tkinter


    【解决方案1】:

    你可以使用:

    root.wm_state("iconic")
    

    例如,执行此操作的按钮可能是:

    tk.Button(root, text = "Mimimize", command = lambda: root.wm_state("iconic")).pack()
    

    所以在你的例子中应该是:

    def minim():
        root.wm_state("iconic")
    

    使用标准模块(根据要求)。在 Python 3.6 中在 Windows 10 中进行了测试,但它应该适用于所有操作系统和 Python 版本。

    【讨论】:

    • 您好,已使用:> root.overrideredirect(0) 但是当我最大化返回时,它给了我另一个问题,我需要一种方法将其设置回 root.overrideredirect(1)最大化时。那怎么做? (对不起我的愚蠢问题,我只是从 python 上的 GUI 开始,thanx 提前)。
    • @Marcel Barlik 澄清一下,您的意思是最大化还是未最小化? (用应用程序填充屏幕与在栏中按下应用程序)
    • 现在好了。我找到了办法。感谢您的帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 2017-12-02
    • 2014-08-26
    • 2019-06-15
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多