无法在函数 tkinter 中显示图像

问题描述 投票:0回答:1

这是我的代码

def new_grille():
  global cc
  global tab
  global tableau

  x = cc[0]
  y = cc[1]


  fenetre = tk.Tk()
  fenetre.title('Gomoku : Coup joueur Blanc')
  fenetre.attributes('-fullscreen', True)
  fenetre.bind('<Escape>', lambda e: fenetre.destroy())
  fenetre.config(bg='#FFFFFF')


  tableau = tk.Canvas(fenetre, width = 1700, height = 1500, bg ="#FFFFFF")
  tableau.pack(side = tk.BOTTOM, pady=100)

  img = Image.open('boutton quitter.png')
  img_quitter = ImageTk.PhotoImage(img)


  boutton_quitter = tk.Button(fenetre, width=45, height=7,image = img_quitter, command=lambda x="": on_closing())
  boutton_quitter.image = img_quitter
  boutton_quitter.place(x=1500, y=100)
  boutton_quitter.pack()

我试图在“boutton_quitter”按钮上显示图像(“boutton_quitter.png”),但它引发错误(_tkinter.TclError:图像“pyimage13”不存在),我尝试了很多方法但没有任何效果,请帮助我:)

我看到了不同的东西,顶级方法并试图将图像存储在全局函数中,但要么我搞砸了,要么它不起作用

python image function tkinter tkinter-button
1个回答
0
投票

您不能同时使用布局管理器 boutton_quitter.place(x=1500, y=100) boutton_quitter.pack()。使用一个布局管理器来满足您的需要

使用时

fenetre.attributes('-fullscreen', True)
请勿设定。布局管理器值太高,例如
place()
pack()
可以满足您的需求。

我评论说我们不需要。

片段:

import tkinter as tk
from PIL import Image, ImageTk

#cc = 0

def new_grille():
  #global cc
  #global tab
  global tableau


  fenetre = tk.Tk()
  fenetre.title('Gomoku : Coup joueur Blanc')
 # fenetre.attributes('-fullscreen', True)
  fenetre.bind('<Escape>', lambda e: fenetre.destroy())
  fenetre.config(bg='#FFFFFF')


  tableau = tk.Canvas(fenetre, width = 700, height = 500, bg ="#FFFFFF")
  tableau.pack(side = tk.BOTTOM, pady=100)

  img = Image.open('p2.png')
  img_quitter = ImageTk.PhotoImage(img)

  boutton_quitter = tk.Button(fenetre, width=250, height=350,image = img_quitter, command=lambda x="": on_closing())
  boutton_quitter.image = img_quitter
  boutton_quitter.place(x=100, y=100)
  #boutton_quitter.pack()
  fenetre.mainloop()

if __name__ == "__main__":
    new_grille()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.