gpt4 book ai didi

python - 如何在点击时更改 Tkinter 按钮图像?

转载 作者:行者123 更新时间:2023-12-04 14:03:49 24 4
gpt4 key购买 nike

我有一个按钮有两个图像,我还创建了带有图像的按钮。我想在第一次单击时更改按钮图像。例如,当我单击按钮时,我希望按钮图像将“UnclickedImage”更改为“ClickedImage”。

我无法在 stackoverflow 中找到任何类似的问题。

from tkinter import *


class Example(Frame):

def __init__(self, tk=None):
super().__init__()
self.tk = tk
self.init_ui()

def init_ui(self):

self.photoTodo = PhotoImage(file="./Images/TodoUnClicked.png")
Button(self.tk, image=self.photoTodo).pack(side=LEFT)
self.photoStayFocussed = PhotoImage(file="./Images/StayFoccusedUnClicked.png")
Button(self.tk, image=self.photoStayFocussed).pack(side=RIGHT)


def main():

root = Tk()
root.configure(bg='white')
ex = Example(root)
root.title('')
root.iconbitmap('./Images/empty.ico')
root.geometry("400x100+300+300")
root.mainloop()


if __name__ == '__main__':
main()

最佳答案

您可以使用自定义按钮类来完成此操作。

class ImageButton(Button):
def __init__(self, master, image1, image2, *args, **kwargs):
self.unclickedImage = PhotoImage(file = image1)
self.clickedImage = PhotoImage(file = image2)
super().__init__(master, *args, image = self.unclickedImage, **kwargs)
self.toggleState = 1
self.bind("<Button-1>", self.clickFunction)
def clickFunction(self, event = None):
if self.cget("state") != "disabled": #Ignore click if button is disabled
self.toggleState *= -1
if self.toggleState == -1:
self.config(image = self.clickedImage)
else:
self.config(image = self.unclickedImage)

这将 widget master 以及第一和第二个图像路径作为参数,创建 PhotoImage对象,然后用未点击的图像实例化一个按钮。
有一个绑定(bind)到 <Button-1> ,这是左键单击,这会在单击按钮时更改图像。 toggleState变量会跟踪当前显示的图像,因此当单击按钮时会显示相反的图像。
您可以像这样将其合并到当前程序中:

from tkinter import *

class ImageButton(Button):
def __init__(self, master, image1, image2, *args, **kwargs):
self.unclickedImage = PhotoImage(file = image1)
self.clickedImage = PhotoImage(file = image2)
super().__init__(master, *args, image = self.unclickedImage, **kwargs)
self.toggleState = 1
self.bind("<Button-1>", self.clickFunction)
def clickFunction(self, event = None):
if self.cget("state") != "disabled": #Ignore click if button is disabled
self.toggleState *= -1
if self.toggleState == -1:
self.config(image = self.clickedImage)
else:
self.config(image = self.unclickedImage)

class Example(Frame):

def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.init_ui()

def init_ui(self):
ImageButton(self, "./Images/TodoUnClicked.png", "./Images/TodoClicked.png").pack(side=LEFT)
ImageButton(self, "./Images/StayFoccusedUnClicked.png", "./Images/StayFoccusedClicked.png").pack(side=RIGHT)


def main():

root = Tk()
root.configure(bg='white')
ex = Example(root)
ex.pack(fill = "both", expand = True)
root.title('')
root.geometry("400x100+300+300")
root.mainloop()


if __name__ == '__main__':
main()

请注意,我还修复了您的 Example类,因为您没有正确使用它。当你继承一个 tkinter 对象时,你将 master 传递给 super().__init__然后使用 self作为 child 的 parent 。

关于python - 如何在点击时更改 Tkinter 按钮图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69167947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com