0

I don't know anything about this subject, I don't have any example code, all I can give you is my goal. At the moment, I am just trying to add images that I can see but in the future I want to be able to use them as buttons(I was thinking I could bind them some way). Another thing that might me helpful to know is that I am running this on a Mac.

Simplified questions:

1.How do I add an Image that I can see

2.How do I make this image into a button(with binding)

If you have an answer to these questions, please send me some code I can try out, Thanks!

3
  • 1
    Possible duplicate of how to add an image to a button in tkinter? Commented Jun 14, 2017 at 19:54
  • Possible duplicate: stackoverflow.com/questions/37515847/… Edit WE are the duplicates... Commented Jun 14, 2017 at 19:54
  • It sounds like the first thing you need to do is work through a Tkinter tutorial. This question is way too broad, and is documented in many places on the web. Commented Jun 14, 2017 at 20:44

2 Answers 2

1

This will do the work:

from tkinter import *

root = Tk()

myImg = PhotoImage(file= "photoTry.png") 

btn= Button(root, image=myImg)
btn.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Well I should have mentioned I am on a Mac(buttons are uniform, you can't change height) so I use Frames instead and bind them to turn them into a button. So when I changed it to a Frame and changed the file to what I want to open, I got this error: '_tkinter.TclError: couldn't open "view.png": no such file or directory'
0

This loads an image into a label using PIL.

from PIL import Image, ImageTk

image = Image.open("image.jpg")
photo = ImageTk.PhotoImage(image)

label = Label(image=photo)
label.image = photo
label.pack()

This is how you can add an image to a button.

import tkinter as tk
from PIL import ImageTk

root = tk.Tk()
def make_button():
    b = tk.Button(root)
    image = ImageTk.PhotoImage(file="1.png")
    b.config(image=image)
    b.image = image
    b.pack()
make_button()
root.mainloop()

1 Comment

What is PIL, I thought that makes your programs not universal but I may be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.