Image behind buttons in tkinter (PhotoImage)

前端 未结 1 434
广开言路
广开言路 2021-01-06 20:33

I\'ve been trying to add an image so that my buttons sit on top of the image, but have only been able to make the image cover everything completely or force the image to be

相关标签:
1条回答
  • 2021-01-06 20:59

    You could place an image on a canvas, and then place a button on the canvas:

    import Tkinter as tk
    import ImageTk
    
    FILENAME = 'image.png'
    root = tk.Tk()
    canvas = tk.Canvas(root, width=250, height=250)
    canvas.pack()
    tk_img = ImageTk.PhotoImage(file = FILENAME)
    canvas.create_image(125, 125, image=tk_img)
    quit_button = tk.Button(root, text = "Quit", command = root.quit, anchor = 'w',
                        width = 10, activebackground = "#33B5E5")
    quit_button_window = canvas.create_window(10, 10, anchor='nw', window=quit_button)    
    root.mainloop()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题