我想在按钮上显示一个图片,请问是怎么回事(标签-python)

我想在按钮上显示一个图片,但是按照下面代码,图片根本显示不出来,请问是怎么回事



```python

#读取图片 仅用于tk
def get_img(path:str):
    return itk.PhotoImage(Image.open(path,mode='r'))


button = tk.Button(top,text="导入",width=30,height=3,command=imp,image=get_img('B_import.png')).place(x=680,y=380)


如图所示

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/303288320486191.png "#left")

码字不易,有用望采纳 答案参考Chatgpt解答
根据你提供的代码,有几个可能的原因导致图片无法显示在按钮上:

  1. 导入的模块不正确:请确保你已经正确导入了所需的模块,包括tkinterPIL库(PIL库用于处理图像)。正确的导入方式如下:
import tkinter as tk
from PIL import Image, ImageTk
  1. 图片路径不正确:检查图片路径是否正确,并确保图片文件存在。可以使用绝对路径或相对路径指定图片的位置。确保path参数传递给get_img函数的值是正确的。

  2. 图片对象的生命周期问题:在你的代码中,get_img函数返回一个PhotoImage对象,但该对象没有被引用保存下来,而是直接传递给了Buttonimage参数。这可能导致垃圾回收机制将其清除,从而导致图片无法显示。你可以将get_img函数返回的PhotoImage对象保存到一个变量中,确保它在整个应用程序的生命周期内都保持有效。

以下是修正后的代码示例:

import tkinter as tk
from PIL import Image, ImageTk

def get_img(path: str):
    return ImageTk.PhotoImage(Image.open(path, mode='r'))

top = tk.Tk()
button_image = get_img('B_import.png')
button = tk.Button(top, text="导入", width=30, height=3, command=imp, image=button_image)
button.place(x=680, y=380)

top.mainloop()

在修复后的代码中,get_img函数返回的PhotoImage对象被保存在了button_image变量中,并传递给了Buttonimage参数。这样就保证了PhotoImage对象在整个应用程序的生命周期内有效,从而能够正确显示图片。

请注意,以上代码仅为修复了图片显示问题的示例,可能需要根据你的实际情况进行调整。如果问题仍然存在,你可以提供更多的代码和错误信息,以便我能够更具体地帮助你解决问题。

首先你把图片链接放在浏览器地址栏试一下能否查看,如果可以就看一下你的项目是否为http得协议,有可能协议不一致导致


    def fireupPosition(self):
        def resize(nX, nY):
            xNew = 600
            yNew = int(nY * 600 / nX)
            return (xNew, yNew)

        root = tk.Tk()
        root.geometry("900x600")
        root.title('测点位置')
        
        strPng = './xxx.png'
        photo = ImageTk.PhotoImage(file=strPng)
        photoNew = Image.open(strPng).resize(resize(photo.width(), photo.height()))
        photo = ImageTk.PhotoImage(photoNew)
        rootLabel = tk.Label(root, image=photo)
        rootLabel.grid(column=0, row=0)
        root.columnconfigure(0, weight=1)
        root.rowconfigure(0, weight=1)
        root.mainloop()

以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:

你需要将get_img函数返回的PhotoImage对象保存到一个变量中,然后将该变量作为Buttonimage参数传入。另外,place方法返回的是None,所以你需要将place方法单独调用。

img = get_img('B_import.png')
button = tk.Button(top, text="导入", width=30, height=3, command=imp, image=img)
button.place(x=680, y=380)

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
你需要注意几点:

  1. 注意图片路径是否正确,可以在打开图片时加入绝对路径以确保正确的获取图片。

  2. Button 中设置图片只能使用参数 image,不能使用 photoimage

  3. 分离 Button 在窗口上的布局和定义,因为 place 方法返回的是 None,导致 button 变量等同于 None

你可以按照以下代码进行修改:

import tkinter as tk
from PIL import Image, ImageTk

# 读取图片
def get_img(path:str):
    return ImageTk.PhotoImage(Image.open(path))

# 定义窗口
top = tk.Tk()

# 定义按钮
imp_img = get_img('D:\\B_import.png')
button = tk.Button(top, text="导入", image=imp_img, command=None)
button.place(x=680, y=380)

# 运行窗口
top.mainloop()

在这个例子里,我使用了绝对路径读取图片,读取到的是 PIL.Image 类型。接着,我转换为 tkinter.PhotoImage 类型。最后,我将 PhotoImage 类型的图片赋值给按钮组件的 image 参数。

注意到,为了避免 place 方法返回 None,我将 place 方法单独调用,并将按钮组件赋值给变量 button,这样可以对按钮的其他属性进行更改,如颜色、大小等等。

运行这段代码,你应该可以在窗口看到:

![output](https://i.loli.net/2021/12/22/C9P3yhwZS2tUxoz.png

以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:

在按钮上显示图片,可以使用以下几种方法:

  1. 使用 PhotoImage 载入图片,并用 image_name 属性显示在按钮上:
python
from tkinter import *  
from tkinter.ttk import * 

root = Tk()

photo = PhotoImage(file = 'button.png')  
Button(root, image=photo).pack()  

root.mainloop()

  1. 先用 Canvas 绘图,然后在按钮上显示 canvas:
python 
from tkinter import *  

root = Tk()

canvas = Canvas(root, width=50, height=50)  
canvas.pack()  
canvas.create_rectangle(0, 0, 50, 50, fill="blue")

Button(root, image=canvas).pack()

root.mainloop() 

  1. 设置按钮的 image 和 compound 属性,指定图片位置:
python
from tkinter import *  

root = Tk()

photo = PhotoImage(file='button.png')  
b1 = Button(root, image=photo, compound=TOP)  
b1.pack() 

b2 = Button(root,image=photo, compound=BOTTOM)  
b2.pack() 

b3 = Button(root,image=photo, compound=LEFT)  
b3.pack()  

b4 = Button(root,image=photo, compound=RIGHT)  
b4.pack()  

root.mainloop()

  1. 使用 tkinter.ttk.LabelFrame, 设置 labelimage 属性为图片, 然后添加按钮:
python 
from tkinter import *  
from tkinter.ttk import *  
root = Tk()

lf = LabelFrame(root, labelwidget = Label(root, image=photo))  
lf.pack()  

Button(lf, text='Button').pack()

root.mainloop()

以上方法可以实现在按钮上Adding images, 具体可以根据需要选择使用