How to display VideoFileClip in canvas Tkinter - python

I want to display the video in canvas instead opening in a new window.
And I want to know how to stop and pause the video.
import pygame
from Tkinter import *
def movie():
from moviepy.editor import *
pygame.display.set_caption('Hello World!')
clip = VideoFileClip('1.mp4')
clip.preview()
pygame.quit()
root = Tk()
root.title("AVATAR")
label = Label(root, fg="dark green")
label.pack()
frame = Frame(root,background='red')
frame.pack()
canvas = Canvas(height=200,width=200)
canvas.pack()
conversationbutton = Button(frame, text='play in canvas',width=25,fg="green", command=movie)
conversationbutton.pack(side = RIGHT)
stopb=Button(root, text="stop").pack()
Thanks

Related

How can I manage to put a button image into an image background Tkinter

Good day everyone, can I ask how can I manage to put this button inside an image background
code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Japanese Retro Hub")
root.geometry('2200x1000')
#Image
bg = Image.open("RetroHub_BG_Main.png")
resized = bg.resize((2200,1000), Image.ANTIALIAS)
bg = ImageTk.PhotoImage(resized)
bglabel = Label(root, image=bg)
bglabel.pack(pady=20)
#text for my_command and Start exit button
text = Label(root, text= "Hello world")
text.pack(pady=30)
def my_command():
text.config(text="You have clicked Me...")
def start_exit_button():
click_btn = PhotoImage(file='start_button.png')
img_label= Label(image=click_btn)
button = Button(root, image=click_btn,command=my_command,borderwidth=0)
button.pack(padx=50, pady=20)
root.mainloop()
start_exit_button()
root.mainloop()
This is the image example that I am trying to portray
https://drive.google.com/file/d/1VzbhNazEnfOCk0_QZOD_nWR5jsb-a8GB/view?usp=sharing

How to create a new Tkinter window frame within a window?

So basically I am making a GUI with Tkinter. I also want to make a frame within the Tkinter window, and it should open when I click a button.
Here is my code so far:
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
button1 = Button(screen)
button.pack()
screen.mainloop
So how do I make a new window(frame) when I click the button?
You can create/toggle frame following the below logic
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
frame_enabled = False
def toggle_frame():
global frame_enabled
if not frame_enabled:
my_frame.pack(fill='both', expand=True)
else:
my_frame.pack_forget()
frame_enabled = not frame_enabled
button1 = Button(screen, text="Toggle frame", command=toggle_frame)
button1.pack()
my_frame = Frame(screen, bg="red")
screen.mainloop()

How can I call the opencame function when I press the button python

I have a dashboard.py file, when I run it will show a screen containing the button, how can I display the button on the screen and when clicking on it will open the file face_detect, thank you everyone
from tkinter import *
class DashboardWindow():
def __init__(self):
self.win = Tk()
# reset the window and background color
self.canvas = Canvas(self.win, width=600, height=500, bg='white')
self.canvas.pack(expand=YES, fill=BOTH)
# show window in center of the screen
width = self.win.winfo_screenwidth()
height = self.win.winfo_screenheight()
x = int(width / 2 - 600 / 2)
y = int(height / 2 - 500 / 2)
str1 = "600x500+" + str(x) + "+" + str(y)
self.win.geometry(str1)
# disable resize of the window
self.win.resizable(width=False, height=False)
# change the title of the window
self.win.title("WELCOME | Login Window | ADMINISTRATOR")
self.win.mainloop()`enter code here`
def opencame(self):
import face_detect
button = Button(text="OpenCame", font='Courier 15 bold',command = opencame())
button.pack()
try this
from tkinter import *
def opencame():
import face_detect
button = Button(text="OpenCame", font='Courier 15 bold',command = opencame)
button.pack()
button.mainloop()
First you should change command=opencame() to command=opencame.
Second it really depends on how face_detect.py is written. For example, if face_detect.py is like below:
# face_detect.py
import tkinter as tk
win = tk.Toplevel()
win.transient(win.master)
tk.Label(win, text='welcome to face detection', font='Helvetica 16 bold').pack()
tk.Button(win, text='Quit', command=win.destroy).pack()
win.grab_set()
win.wait_window()
A window will be shown if OpenCame button is first clicked. However no window will be shown when the button is clicked again because the module has already been loaded.
However, if you rewrite face_detect.py as below:
# face_detect.py
import tkinter as tk
def open_window():
win = tk.Toplevel()
win.transient(win.master)
tk.Label(win, text='welcome to face detection', font='Helvetica 16 bold').pack()
tk.Button(win, text='Quit', command=win.destroy).pack()
win.grab_set()
win.wait_window()
and rewrite opencame() as below:
def opencame(self):
import face_detect
face_detect.open_window()
Then a window will be shown whenever the OpenCame button is clicked.

can't invoke "wm" command: application has been destroyed

import tkinter as tk
from tkinter import *
import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
wall_label = Label(image = wall)
root.geometry(f"{wall.width()}x{wall.height()}")
root.overrideredirect(True)
currentMouseX,currentMouseY = pyautogui.position()
print(f'{currentMouseX} {currentMouseY}')
root.geometry("+0+0")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
wall_label.place(x = -2,y = -2)
root.after(1000 , root.destroy)
#root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.mainloop()
root.after(1000 , root.destroy)
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
Please understand even if the source code is dirty.
What I want is for the image to follow the mouse
I tried to follow the mouse by repeating the image disappearing and appearing, but it is not easy.
Here. I made the image follow the mouse, without pyautogui.
Code:
import tkinter as tk
from tkinter import *
#import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
c = Canvas(root, height = 1200, width = 1200)
c.pack()
pic = c.create_image(600, 600, image = wall)
def movepic(event):
x,y = event.x, event.y
c.coords(pic, x, y)
c.bind_all("<Motion>", movepic)
root.mainloop()

Adding an image to a button in Tkinter

I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()

Categories

Resources