0

I am so new to python. I am trying to implement a program which changes a button's image property with an "on click" operation. I have tried png, jpeg, gif and bmp formats. I don't want to use PIL, I just want to handle it with tkinter. When I click on the button a small square appears on button but there is no actual image that can be seen. I have tried many things, search StackOverflow and the Internet, and couldn't come up with a solution. Is the problem caused by the version problem or something? My code is below, thank you so much for your support:

from tkinter import *


  def insertRedDisc(buttonParam):
    image = PhotoImage(file=r"C:\Connect4\red.png")
    buttonParam['image'] = image

  window = Tk()
  window.title("Button State App")
  window.geometry('1000x1000')
  window['bg'] = "blue"
  for i in range(6):
   for j in range(7):
    frame = Frame(master=window, relief=FLAT, borderwidth=5, bg="blue")
    frame.grid(row=i, column=j, padx=1, pady=1)
    myButton = Button(master=frame, width=5, height=5)
    myButton['command'] = lambda theButton=myButton: insertRedDisc(theButton)
    myButton.pack()
  window.mainloop()

App currect image

BCZ
  • 1
  • 1
    The image is not shown is due to the notorious issue ["Why does tkinter image not show up if created in a function"](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). Even you fix the issue, the size of the button is still tiny because you have set `width=5` and `heigh=5`. After an image is used, the unit of these options will be in pixels, i.e the button will be changed to 5x5 pixels button. – acw1668 Nov 12 '21 at 10:00
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). Also, be sure in the future to provide correctly formatted code in your questions. The indentation in this code is messed up; if I run this, I get an `IndentationError`, and that's not what the question was asking about. See [example] for more information. – Sylvester Kruin Nov 12 '21 at 18:01

0 Answers0