首页 > 解决方案 > 如何判断按钮是否在 tkinter 中被抬起

问题描述

我问如果 Tkinter 窗口中有几个按钮,其中一些按钮是 FLAT 并且一些按钮是 RAISED 的。我想要一个函数来知道按钮是凸起的还是平的。

请帮忙

提前致谢

标签: pythonpython-3.xtkinter

解决方案


解决方案:

tkinter按钮具有浮雕属性,指定按钮是否为SUNKEN、RAISED、GROOVE 和 RIDGE

这是您应该如何检查按钮是凸起还是平坦的:

import tkinter as tk

def RaisedOrFlat(buttonName):
    button = buttonName
    # check if the button is Raised
    if button['relief']== "raised": 
        return 'raised'
    # check if the button is flat
    elif button['relief'] == "flat": 
        return 'flat'
#Creates the form
main = tk.Tk()
#creates the button
btn1 = tk.Button(main, text='Button 1', relief="raised")
btn1.pack()

#call the function
if RaisedOrFlat(btn1) == 'flat':
    print('flat')
elif RaisedOrFlat(btn1) == 'raised':
    print('raised')

main.mainloop()

输出raised


推荐阅读