Skip to main content
 首页 » 编程设计

python之由图像制成的按钮之Tkinter

2024年11月01日8findumars

我正在尝试制作一个使用图像的 Tkinter 按钮,单击时将执行函数 on_button()。我当前的代码如下所示。

b = Button(root, image = play, command = on_button) 

但是,这只是一个周围有灰色按钮轮廓的图像。我希望能够只有一个可以单击以执行 on_button 的图像,但不知道如何执行此操作。

请您参考如下方法:

使用 relief 选项。

b = Button(root, image=play, command=on_button, relief=FLAT) 

更新

根据@BryanOakley 的说法,在 OSX 上,带有 relief=FLAT 的按钮不起作用。使用 Label 小部件:

lb = Label(root, image=play) 
lb.bind('<Button-1>', lambda e: on_button) 
lb.pack()