Adding an image to a button in Tkinter

后端 未结 2 1036
太阳男子
太阳男子 2021-01-13 18:02

I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can\'t even see the but

2条回答
  •  长情又很酷
    2021-01-13 18:31

    You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.

    button_qwer = Button(root, text="asdfasdf", image=imagetest)
    button_qwer.pack()
    

    Your full code can be like:

    from tkinter import *
    import tkinter as tk
    
    root = tk.Tk()
    root.geometry("960x600")
    
    canvas = Canvas(root, width=500, height=500)
    canvas.pack()
    
    imagetest = PhotoImage(file="giftest.gif")
    canvas.create_image(250, 250, image=imagetest)
    
    button_qwer = Button(root, text="asdfasdf", image=imagetest)
    button_qwer.pack()
    root.mainloop()
    

提交回复
热议问题