简体   繁体   English

单击Python时的Tkinter救济按钮

[英]Tkinter relief buttons when clicked python

So, in Tkinter Buttons seem to have a relief property. 因此,在Tkinter中,Button似乎具有释放特性。 This is defaulted to raised, and when the button is pressed it changes to sunken. 默认情况下为凸起,当按下按钮时,它变为凹陷。 I made my use relief=FLAT, but when I click this button it still is sunken. 我已将使用缓解设置为FLAT,但单击此按钮时它仍然沉没。 Is there a way to make it so that there is a different relief when clicked? 有没有一种方法可以使单击时的效果有所不同? Would it involve changing the border color? 是否会涉及更改边框颜色?

Button(text=day, width=2,
       relief=FLAT, activebackground = self.color_clicked,
       background = self.today_color)

This looks flat, but when I click on it it changes to sunken (obviously it changes back to flat when I let go, however). 这看起来很平坦,但是当我单击它时它会变成凹陷(显然,放开时它会变回平坦)。

You can tie a mouse-click to a function that enforces the relief of the button to keep it flat, otherwise the mouse-click will use its default action on click: making the button sunken. 您可以将鼠标单击绑定到强制释放按钮以使其平整的函数,否则鼠标单击将在单击时使用其默认操作:使按钮沉没。

def keep_flat(event):       # on click,
    if event.widget is btn: # if the click came from the button
        event.widget.config(relief=FLAT) # enforce an option

def callback():
    print('button clicked')

root = Tk()

btn = Button(root, text='click', relief=FLAT, command=callback)
btn.pack()

root.bind('<Button-1>', keep_flat) # bind the application to left mouse click

mainloop()

Another option is to bind the button-click to the string "break", which prevents any typical event handlers from kicking off (including the button's command option): 另一个选择是将按钮单击绑定到字符串“ break”,这可以防止任何典型的事件处理程序启动(包括按钮的command选项):

btn.bind('<Button-1>', lambda e: 'break')

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Setting -borderwidth (-bd) to 0 worked for me. -borderwidth (-bd)设置为0对我(-bd) The button text keeps moving down showing the button is being pressed, but sunken border doesn't appear. 按钮文字一直向下移动,显示按钮被按下,但没有出现下沉的边框。

Binding the mouse-click function is not the correct solution because the configuration code is executed after the button-down relief is displayed. 绑定鼠标单击功能不是正确的解决方案,因为在显示按下按钮后会执行配置代码。 Bryan's suggestion to use a label seems to be the only way to accomplish this simply. Bryan关于使用标签的建议似乎是简单地完成此操作的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM