Tkinter inserting video into window [duplicate] - python

This question already has an answer here:
Video output within Tkinter?
(1 answer)
Closed 6 years ago.
I currently have this code for opening up a .mp4 file from a tkinter program.
import os
from tkinter import *
app = Tk()
app.title('Video Player')
Fcanvas = Canvas(bg="black", height=600, width=170)
def snd1():
os.system("C:\\Users\Burky\\Desktop\\Videos\\PermsAndCombsVideo.mp4")
var = IntVar()
rb1 = Radiobutton(app, text= "Play Video", variable = var, value=1, command=snd1)
rb1.pack(anchor = W)
Fcanvas.pack()
app.mainloop()
This is good, although the video opens up within quick-time player and is not embedded within the window, is there a way to implement this into the main window instead of it opening up in the quick-time player?
thanks

Here's one way to do it. This will continuously update the labels image to be the frames of the video specified. You'll have to handle sound if you want that too. This should get you started.
import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk
video_name = "test.mkv" #This is your video file path
video = imageio.get_reader(video_name)
def stream(label):
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
if __name__ == "__main__":
root = tk.Tk()
my_label = tk.Label(root)
my_label.pack()
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
root.mainloop()

Related

Tkinter - Python3 Does Not Show The Image When Clicked [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 8 months ago.
I've tried several methods but did not worked. Suppose I am working on a project that changes the picture when the button is clicked.
Well as I know, all it did is I just clicked 1 time and the image turned white and I can not click it anymore. Quick! I need to complete this project in 1 hour deadline. Here is the code.
# Modules
from tkinter import *
from PIL import Image, ImageTk
# Functions
def change_img():
global curr_img
global root
global image1
image1 = Image.open(f"Hamster {curr_img + 1}.jpg")
image1 = image1.resize((100, 60))
img1 = ImageTk.PhotoImage(image1)
btn.configure(image=img1, command=change_img)
root.update()
curr_img += 1
# Main Window
root = Tk()
root.title("Picture Change")
root.geometry("300x150")
root.config(bg="black")
# Adding Objects
curr_img = 1
Label(root, text="My Favorite Animal", fg="white", bg="black", font=("Arial bold", "20")).pack() # This is a label.
image1 = Image.open("Hamster 1.jpg") # First we open the image and save it into a variable.
image1 = image1.resize((100,60)) # Resizes the image.
img1 = ImageTk.PhotoImage(image1) # Create a ImageTK object from the image1 variable.
btn = Button(image = img1, command=change_img) # Create a label and put the image into it.
btn.pack() # Pack the label that contains the image. Now, we repeat the process.
# Mainloop
root.mainloop()
Thanks if the answer is quick and accurate. Comment, thanks very much too.
You create the next image in the change_img() function and when the function exits the reference to the image is garbage collected. You can save a reference to the image in the button widget:
btn.configure(image=img1, command=change_img)
btn.image = img1 # Save a reference to the new image

Tkinter images crashing

So I made a script in python with Tkinter and the thing is that the first Tkinter window pops up without problems but when the code goes to the second window it says :
_tkinter.TclError: image "pyimage1" doesn't exist
and I didn't find anything that helped me, could someone help me please ?
Here is the code :
from Tkinter import *
from PIL import ImageTk, Image
def choose():
global name, chosen
name = name1.get()
chosen = chosen1.get()
print name
print chosen
root0.quit()
root0 = Tk()
name1 = Entry(root0)
name1.pack()
chosen1 = Entry(root0)
chosen1.pack()
Button(root0, text="ENTER", command=choose).pack()
root0.mainloop()
root = Tk()
img = ImageTk.PhotoImage(Image.open('person1.png'))
panel1 = Label(root, image = img)
panel1.pack(side="left")
img2 = ImageTk.PhotoImage(Image.open('person2.png'))
panel2 = Label(root, image = img2)
panel2.pack(side="right")
root.mainloop()
by the way, the python version is 2.7
This is a side effect of using 2 roots (Tk() instances). The images default to associate with the first root window. The quick fix is to provide the image with the correct root:
img2 = ImageTk.PhotoImage(Image.open('person2.png'), master=root)
The proper fix is to never use more than one Tk(). Put all your code into Frame instances, and then destroy one and load the other when the time is right:
import Tkinter as tk
def choose():
global name, chosen
name = name1.get()
chosen = chosen1.get()
print name
print chosen
frame0.destroy() # kill this frame
frame1.pack() # open new frame
root = tk.Tk()
frame0 = tk.Frame(root)
name1 = tk.Entry(frame0)
name1.pack()
chosen1 = tk.Entry(frame0)
chosen1.pack()
tk.Button(frame0, text="ENTER", command=choose).pack()
frame1 = tk.Frame(root)
img = ImageTk.PhotoImage(Image.open('person1.png'))
panel1 = tk.Label(frame1, image = img)
panel1.pack(side="left")
img2 = ImageTk.PhotoImage(Image.open('person2.png'))
panel2 = tk.Label(frame1, image = img2)
panel2.pack(side="right")
#start the program
frame0.pack() # load frame0
root.mainloop()
Note I also moved you away from the evil wildcard imports (from module import *).

how can I exit a tkinter window after changing images?

So I'm trying to make a little battle scene using Tkinter, the code is supposed to change the image, wait a couple of seconds, then exit the Tkinter window. The code I have just makes a little pause when the button to change images is pressed. I'm still a beginner and some concepts are hard for me to grasp.
Here is the code:
from tkinter import *
import time
class MainWindow():
def __init__(self, main):
# canvas for image
self.canvas = Canvas(main, width=660, height=440)
self.canvas.grid(row=0, column=0)
# images
self.my_images = []
self.my_images.append(PhotoImage(file = "att1.gif"))
self.my_images.append(PhotoImage(file = "att2.gif"))
self.my_image_number = 0
# set first image on canvas
self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images[self.my_image_number])
# button to change image
self.button = Button(main, text="FIGHT", command=self.onButton)
self.button.grid(row=1, column=0)
#----------------
def onButton(self):
# next image
self.my_image_number = 1
if self.my_image_number == 1:
time.sleep(2)
root.quit()
# change image
self.canvas.itemconfig(self.image_on_canvas, image = self.my_images[self.my_image_number])
root = Tk()
MainWindow(root)
root.mainloop()
some of this code is borrowed, I tried to alter it to fit my purpose
The image is not changed because time.sleep(2) blocks tkinter update. After the sleep, tkinter quit and so the image is not updated.
Since you have only 2 images and you want to exit the tkinter window 2 seconds after the change of image, try:
def onButton(self):
self.canvas.itemconfig(self.image_on_canvas, image=self.my_images[1])
root.after(2000, root.destroy) # close root window after 2 seconds

How to display a animated GIF in a Tkinter Label - Python [duplicate]

This question already has answers here:
Play an Animated GIF in python with tkinter
(3 answers)
Closed 5 years ago.
I've been trying to play an animated gif using Tkinter.PhotoImage, but haven't been seeing any success. It displays the image, but not the animation. The following is my code:
root = Tkinter.Tk()
photo = Tkinter.PhotoImage(file = "path/to/image.gif")
label = Tkinter.Label(image = photo)
label.pack()
root.mainloop()
It displays the image in a window, and that's it. I'm thinking that the issue has something to do with Tkinter.Label but I'm not sure. I've looked for solutions but they all tell me to use PIL (Python Imaging Library), and it's something that I don't want to use.
With the answer, I created some more code (which still doesn't work...), here it is:
from Tkinter import *
def run_animation():
while True:
try:
global photo
global frame
global label
photo = PhotoImage(
file = photo_path,
format = "gif - {}".format(frame)
)
label.configure(image = nextframe)
frame = frame + 1
except Exception:
frame = 1
break
root = Tk()
photo_path = "/users/zinedine/downloads/091.gif"
photo = PhotoImage(
file = photo_path,
)
label = Label(
image = photo
)
animate = Button(
root,
text = "animate",
command = run_animation
)
label.pack()
animate.pack()
root.mainloop()
Thanks for everything! :)
You have to drive the animation yourself in Tk. An animated gif consists of a number of frames in a single file. Tk loads the first frame but you can specify different frames by passing an index parameter when creating the image. For example:
frame2 = PhotoImage(file=imagefilename, format="gif -index 2")
If you load up all the frames into separate PhotoImages and then use timer events to switch the frame being shown (label.configure(image=nextframe)). The delay on the timer lets you control the animation speed. There is nothing provided to give you the number of frames in the image other than it failing to create a frame once you exceed the frame count.
See the photo Tk manual page for the official word.
Here's a simpler example without creating an object:
from tkinter import *
import time
import os
root = Tk()
frameCnt = 12
frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
if ind == frameCnt:
ind = 0
label.configure(image=frame)
root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()

tkinter doesn't display image from outside the main file in a label [duplicate]

I expect the same output for both of the scripts below.
But I don't get the image on the button when I execute Script 1. However, Script 2 works well.
Script 1
from Tkinter import *
class fe:
def __init__(self,master):
self.b=Button(master,justify = LEFT)
photo=PhotoImage(file="mine32.gif")
self.b.config(image=photo,width="10",height="10")
self.b.pack(side=LEFT)
root = Tk()
front_end=fe(root)
root.mainloop()
Script 2
from Tkinter import *
root=Tk()
b=Button(root,justify = LEFT)
photo=PhotoImage(file="mine32.gif")
b.config(image=photo,width="10",height="10")
b.pack(side=LEFT)
root.mainloop()
The only reference to the image object is a local variable. When __init__ exits, the local variable is garbage collected so the image is destroyed. In the second example, because the image is created at the global level it never goes out of scope and is therefore never garbage collected.
To work around this, save a reference to the image. For example, instead of photo use self.photo.
its work
x1=Button(root)
photo=PhotoImage(file="Re.png")
x1.config(image=photo,width="40",height="40",activebackground="black"
,bg="black", bd=0,command=sil)
x1.place(relx=1,x=5, y=-5, anchor=NE)
but this is useless
def r():
x1=Button(root)
photo=PhotoImage(file="Re.png")
x1.config(image=photo,width="40",height="40",activebackground="black",
bg="black", bd=0,command=sil)
x1.place(relx=1,x=5, y=-5, anchor=NE)
r()
logo = PhotoImage(file = 'mine32.gif')
small_logo = logo.subsample(5, 5)
self.b.config(image = small_logo , compound = LEFT )
Unrelated answer, but this is the answer I was looking for when I first came here. Use this to resize the image before adding it to the button.
from PIL import Image, ImageTk
image = Image.open("path/to/image.png")
image = image.resize((25, 25), Image.ANTIALIAS)
self.reset_img = ImageTk.PhotoImage(image)
self.button = tk.Button(frame, image=self.reset_img)
from tkinter import *
root= Tk()
btnPlay = Button(root)
btnPlay.config(image=imgPlay, width="30", height="30")
btnPlay.grid(row=0, column=0)
root.mainloop()

Categories

Resources