简体   繁体   中英

Python: preventing Tkinter buttons from having an “animation”

While writing a simple interactive tic tac toe program in Python, using Tkinter to make a GUI, I faced a problem when coding a button.

Whenever you press a button in a Tkinter-based GUI, the button has this "animation" where as long as it's pressed in, it "jumps" slightly downwards and to the right - and when it's released, it jumps back to its original form. I was wondering whether it's possible to prevent this animation from taking place, but still have the button execute a command while being pressed (in other words: function as a button, but look like a plain image).

If it's worth anything, my relevant code is as follows:

import tkinter
from PIL import Image, ImageTk

window = tkinter.Tk()
button_image = ImageTk.PhotoImage(Image.open(<path>))
button = tkinter.Button(window, image=button_image, borderwidth=0)
button.grid(row=1)

window.mainloop()

Thank you in advance.

Maybe instead of a button a label?

panel = Label(root, image=button_image)
panel.image = button_image
panel.grid(row=1)

I have a new answer, what if you do this?

button_image = ImageTk.PhotoImage(Image.open(<path>))
for _ in range(5):
    button = tkinter.button(root, image=button_image, borderwidth=0, relief=SUNKEN)
    button.grid(row=1)
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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