a co ma robic ta funcja clickT()
? przerobiłem swój kod żeby sie kompilował i zadziałał moze z tego kodu cos Ci sie przyda wystarczy dodac sciezke do pliku obrazka..
import tkinter as tk
class Tail(tk.Button):
def __init__(self, master, image=None, x=0, y=0, **kwargs):
# Provide default values for image, x, and y
super().__init__(master, image=image, command=lambda: clickT(self), **kwargs)
self.x = x
self.y = y
# Load the image (replace 'path/to/your/image.gif' with your actual image path)
self.image = tk.PhotoImage(file='images/apple-touch-icon.png')
def setPos(self, x, y):
self.x = x
self.y = y
def setImage(self, image):
self.config(image=image)
def clickT(self):
# Assuming you want to swap to a different image on click (optional)
self.config(image=self.image)
root = tk.Tk()
# Initial state without image
button = Tail(root, image=None, x=100, y=50)
# diffrent image
# another_image = tk.PhotoImage(file='path/to/another/image.png')
# button.setImage(another_image)
button.pack()
root.mainloop()
jesli jest ona poza classa to juz nie odnosimy sie do classy tylko musimy przekazac reference do tej funkcji z uzyciem lambdy, jesli planujesz dodatkowe arg. cos jak tu. no i funkcja musi miec przekazane instacje obiektu jesli chcesz operowac na tym samym obiekcie. (czyli wystarczylo dodac lambde jesli chcesz operowac na dodatkowych zmiennych )
import tkinter as tk
class MyButton(tk.Button):
def __init__(self, master, text, function_to_call, additional_arg):
super().__init__(master, text=text, command=lambda: function_to_call(self, additional_arg))
def setPos(self,x,y):
self.x = x
self.y = y
def setImage(self,image):
self.config(image=image)
def print_message(button_instance, message):
print(f"Message from button: {message}")
root = tk.Tk()
button = MyButton(root, text="Click me", function_to_call=print_message, additional_arg="Hello")
button.pack()
root.mainloop()