How to add image in a button - python

This makes an image:
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()
How do I put image inside a button?

You can put the image on a button.
button1=Button(window , text = 'Click Me !', image = render)

You can use the following code...
Button(master, text = "Button", image = "image.png", compound=LEFT)

You can use the following code ....
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()

Related

Tkinter Display picture when calling function

I wanted to display a picture .png in tkinter when calling a function (but it could also be with a boolean showOnOff).
This is what I wrote for the moment, but the picture doesn't appear. Do you have any idea ?
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
def display():
# Use library PIL to display png picture
path = '3d.png'
img = ImageTk.PhotoImage(Image.open(path), Image.ANTIALIAS)
panel = Label(root, image = img)
panel.grid(row=1, column=0)
ButtonDisplay = ttk.Button(root, text="Display", command=display)
ButtonDisplay.grid(row=0, column=0)
root.mainloop()
The problem is the definition of the image which is only local inside the funtion. If you make the image global, it works:
def display():
global img
# Use library PIL to display png picture
path = '3d.png'
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img)
panel.grid(row=1, column=0)

Display an image tkinter macOSX [duplicate]

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg.
#!/usr/bin/python
import Image
import Tkinter
window = Tkinter.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
imageFile = "Aaron.jpg"
window.im1 = Image.open(imageFile)
raw_input()
window.mainloop()
Try this:
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
path = "Aaron.jpg"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
Related docs: ImageTk Module, Tkinter Label Widget, Tkinter Pack Geometry Manager
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
win = tk. Tk()
image1 = Image. open("Aoran. jpg")
image2 = ImageTk. PhotoImage(image1)
image_label = ttk. Label(win , image =.image2)
image_label.place(x = 0 , y = 0)
win.mainloop()
from tkinter import *
from PIL import ImageTk, Image
window = Tk()
window.geometry("1000x300")
path = "1.jpg"
image = PhotoImage(Image.open(path))
panel = Label(window, image = image)
panel.pack()
window.mainloop()

Display IMAGE and TEXTBOX simultanously in a "form" with PYTHON

Here what I'd like
I'm new in PYTHON. I'm trying to display IMAGE and TEXTBOX simultaneously in a "Form" with PYTHON.
My problem: Image is not visible on the screen. How to solve that problem?
Thanks,
My code:
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400")
window.configure(background='grey')
# Create textbox in window
text_widget = tk.Text(window)
text_widget.insert('insert',"Enter image information here")
text_widget.pack(anchor = "w", padx = 50, pady = 50)
#Creates a tkinter-compatible photo image.
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard tkinter widget used to display a text or
image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
#panel.pack(side = "bottom", fill = "both", expand = "no")
panel.pack()
#Start the GUI
window.mainloop()
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400")
window.configure(background='grey')
#Creates a tkinter-compatible photo image.
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
# Create textbox in window
text_widget = tk.Text(panel)
text_widget.insert('insert',"Enter image information here")
text_widget.pack(anchor = "w", padx = 50, pady = 50)
#The Pack geometry manager packs widgets in rows or columns.
#panel.pack(side = "bottom", fill = "both", expand = "no")
panel.pack()
#Start the GUI
window.mainloop()
If that's the idea, do you wish to display information to the user or for user to enter information?
Assuming the latter, here is something.
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400") # You can drop this line if you want.
window.configure(background='grey')
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
txtVar = tk.StringVar(None)
usrIn = tk.Entry(window, textvariable = txtVar, width = 90)
usrIn.grid(row = 50, column = 60)
usrIn.pack()
panel.pack()
window.mainloop()
ThetxtVar can be used for accepting info from user. You may also have to use the Button feature if needed.
Here is nice link.

Image in tkinter window by clicking on button

I need help about this program, this program should open image in new tkinter window by clicking on button, but it doesn't it just opens new window without image.
Where is the problem?
Using: python 3.3 and tkinter
This is program:
import sys
from tkinter import *
def button1():
novi = Toplevel()
canvas = Canvas(novi, width = 300, height = 200)
canvas.pack(expand = YES, fill = BOTH)
gif1 = PhotoImage(file = 'image.gif')
canvas.create_image(50, 10, visual = gif1, anchor = NW)
mGui = Tk()
button1 = Button(mGui,text ='Sklop',command = button1, height=5, width=20).pack()
mGui.mainloop()
create_image needs a image argument, not visual to use the image, so instead of visual = gif1, you need image = gif1. The next problem is that you need to store the gif1 reference somewhere or else it'll get garbage collected and tkinter won't be able to use it anymore.
So something like this:
import sys
from tkinter import * #or Tkinter if you're on Python2.7
def button1():
novi = Toplevel()
canvas = Canvas(novi, width = 300, height = 200)
canvas.pack(expand = YES, fill = BOTH)
gif1 = PhotoImage(file = 'image.gif')
#image not visual
canvas.create_image(50, 10, image = gif1, anchor = NW)
#assigned the gif1 to the canvas object
canvas.gif1 = gif1
mGui = Tk()
button1 = Button(mGui,text ='Sklop',command = button1, height=5, width=20).pack()
mGui.mainloop()
It's also probably not a good idea to name your Button the same name as the function button1, that'll just cause confusion later on.
from tkinter import *
root = Tk()
root.title("Creater window")
def Img():
r = Toplevel()
r.title("My image")
canvas = Canvas(r, height=600, width=600)
canvas.pack()
my_image = PhotoImage(file='C:\\Python34\\Lib\idlelib\\Icons\\Baba.gif', master= root)
canvas.create_image(0, 0, anchor=NW, image=my_image)
r.mainloop()
btn = Button(root, text = "Click Here to see creator Image", command = Img)
btn.grid(row = 0, column = 0)
root.mainloop()

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.
Currently, I can display an image like so:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
However, when the user hits, say the ENTER key, I'd like to change the image.
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?
The method label.configure does work in panel.configure(image=img).
What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.
The following is the new version:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img.
Another option to do it.
Using object-oriented programming and with an interactive interface to update the image.
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.
Another solution that might be of help.
In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()

Categories