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!

  • 1
    Possible duplicate of [how to add an image to a button in tkinter?](https://stackoverflow.com/questions/37515847/how-to-add-an-image-to-a-button-in-tkinter) – DavidS Jun 14 '17 at 19:54
  • Possible duplicate: https://stackoverflow.com/questions/37515847/how-to-add-an-image-to-a-button-in-tkinter Edit WE are the duplicates... – Carl Shiles Jun 14 '17 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. – Bryan Oakley Jun 14 '17 at 20:44

2 Answers2

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()
Shivam Pandya
  • 251
  • 3
  • 10
  • 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' –  Jun 14 '17 at 21:10
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()
  • What is PIL, I thought that makes your programs not universal but I may be wrong. –  Jun 14 '17 at 21:14