1

Using tkmacosx 1.0.5 I have two buttons. One shows an image. When clicking on the second one, it should show the same image. The image does appear but is shifted to the upper left corner on the second button and therefore becomes partially invisible. How can I avoid that behaviour?

Same behaviour when using place() or pack() instead of grid(). The image is shown correctly when using tkinter instead. However other things don't work with tkinter on macOS, which is why I wanted to use tkmacosx.

How can I use

import tkinter as tk
from tkmacosx import Button

root = tk.Tk()

my_img = tk.PhotoImage(file='img.png')

def pressed(but):
    but.config(image=my_img)

b_1 = Button(root, bg='white', width=600, height=500)
b_2 = Button(root, bg='white', width=600, height=500)

b_1.config(command=lambda b=b_1: pressed(b))
b_2.config(command=lambda b=b_2: pressed(b))

b_1.config(image=my_img)

b_1.grid(column=1, row=1)
b_2.grid(column=2, row=1)

root.mainloop()

This is how it looks before the button is clicked

And this is afterwards

Chris
  • 21
  • 3
  • Where is `but`? `but.config` – toyota Supra Mar 18 '23 at 01:15
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 18 '23 at 06:28
  • 1
    @toyotaSupra `but` is the function argument which is passed by the button command (line 14+15). It is the button object itself. I know that it might seem a bit unneccessary to pass the button object to the function, rather than simply calling the button's name from the function, but this is a very simplified version of a much more complex code, where I do not know the name of the button object. – Chris Mar 18 '23 at 15:47
  • @Community I have edited the question. I hope it is clearer now. The image is not displayed properly on the button. – Chris Mar 18 '23 at 15:56

0 Answers0