【问题标题】:How to add image in a button如何在按钮中添加图像
【发布时间】:2020-10-11 15:04:02
【问题描述】:

这会生成一个图像:

import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.geometry("960x540+480+200")
load = Image.open("example.png")
render = ImageTk.PhotoImage(load)
img = tk.Label(image=render)
img.image = render
img.place(x=450, y=280)
tk.mainloop()

如何将图片放入按钮中?

【问题讨论】:

标签: python button tkinter python-imaging-library tk


【解决方案1】:

您可以将图像放在按钮上。

button1=Button(window , text = 'Click Me !', image = render) 

【讨论】:

  • text='Click Me !' 有什么作用?我在 GUI 上看不到它
  • 因为没有设置compound选项。
【解决方案2】:

您可以使用以下代码...

Button(master, text = "Button", image = "image.png", compound=LEFT)

【讨论】:

  • image="image.png" 将不起作用。 image 参数需要图像,而不是文件名。
【解决方案3】:

您可以使用以下代码....

from tkinter import *
from tkinter.ttk import *

# creating tkinter window 
root = Tk()

# Adding widgets to the root window 
Label(root, text = 'btn').pack(side = TOP, pady = 10) 
 
# Creating a photoimage object to use image 
photo = PhotoImage(file = r"pic.png") 
  
# here, image option is used to set image on button  
Button(root, text = 'button', image = photo).pack(side = TOP)  
 
mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2018-04-21
    • 2014-10-08
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    相关资源
    最近更新 更多