【问题标题】:tkinter how to change a button's look/image in a specific state (disabled)tkinter 如何在特定状态下更改按钮的外观/图像(已禁用)
【发布时间】:2021-08-05 00:22:53
【问题描述】:

我制作了自己的按钮(所以我使用了图像),它就像装饰一样,它必须保持在原处但也不能点击,当我将它设置为"disabled' 状态时,它会得到一个非常丑陋的过滤器就可以了:

如果没有那个丑陋的过滤器,我怎样才能让它无法点击?

【问题讨论】:

  • 不要使用窗口。
  • 那你为什么不直接使用Label 而不是Button
  • 有一种方法可以通过调用overrideredirect 并实施必要的控制来临时移除整个标题栏
  • 尝试使用透明背景的图片,这样就不会有丑陋的滤镜了。

标签: python python-3.x tkinter tkinter-button


【解决方案1】:

这是之前代码的改进版本。

它将暂时删除标题栏,并在必要时将其恢复。

Key F1 现在通过充当切换开关来控制标题栏的可见性。

现在可以随时用Button-1Motion 拾取和移动窗口。

Escape 键或 Alt F4 将关闭窗口。

import tkinter as tk

root = tk.Tk()

def showhide( ev ):
    ev.widget.lift()
    if root.overrideredirect(None):
        ev.widget.overrideredirect( 0 )
    else:
        ev.widget.overrideredirect( 1 )

def drag( ev = None ):
    global xpos, ypos, dragx, dragy, wide, high
    if root.overrideredirect(None):
        if dragx == 0 and dragy == 0:
            dragx, dragy = ev.x, ev.y
        xpos = xpos + ev.x - dragx
        ypos = ypos + ev.y - dragy
        if xpos < 0:  Xpos = 0
        if ypos < 0:  Ypos = 0
        xmax = root.winfo_screenwidth() - wide
        ymax = root .winfo_screenheight() - high
        if xpos > xmax:  xpos = xmax
        if ypos > ymax:  ypos = ymax
        root.geometry( f'+{xpos}+{ypos}')

def clear( ev ):
    global dragx, dragy
    dragx, dragy = 0, 0

def closer( ev = None ):
    root.destroy()

label = tk.Label(
    root,
    text = """Press F1 to hide or show window Title bar
Press Escape key or Alt-F4 to close program
You may now Pickup and drag window around

This movement modification thanks to @titoo

""",
    justify = "left", anchor = "center",
          width = 40, height = 20)
label.pack()

root.resizable( False, False )
root.update()

root.protocol( "WM_DELETE_WINDOW", closer )
root.bind( "<Escape>", closer )

dragx, dragy = 0, 0
xpos, ypos = root.winfo_x(), root.winfo_y()
wide, high = root.winfo_width(), root.winfo_height()

root.bind( "<F1>", showhide )
root.bind( '<B1-Motion>', drag )
root.bind( '<ButtonRelease-1>', clear )

drag( )

root.mainloop()

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 2013-04-09
    • 2011-11-24
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多