0

我对python很陌生。我正在尝试实现一个程序,该程序通过“单击”操作更改按钮的图像属性。我尝试过 png、jpeg、gif 和 bmp 格式。我不想使用 PIL,我只想用 tkinter 处理它。当我单击按钮时,按钮上会出现一个小方块,但看不到实际图像。我尝试了很多东西,搜索 StackOverflow 和互联网,但无法提出解决方案。问题是版本问题还是什么的?我的代码如下,非常感谢您的支持:

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()

应用当前图像

4

0 回答 0