Python Forum

Full Version: Issue getting mouse location
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

The aim of the code below is to get the mouse location on the canvas when its clicked. The canvas also can play a video when the user wants to. I can get the mouse location fine when there is no video playing. Bt when there is a video on top I can no longer get the emouse location when I click. Why is this?

from tkinter import *
from tkinter.filedialog import askopenfile
from tkVideoPlayer import TkinterVideo
from random import *
import cv2
from tkinter import messagebox

global closed
closed = 0

window = Tk()
window.title("Tkinter Play Videos in Video Player")
window.attributes('-fullscreen',True)
window.configure(bg="orange red")

#TODO: get coords of where shapes to go and output onto new vid

canvas = Canvas(window, bg="orange red")
canvas.pack(expand=True, fill=BOTH)

def get_coords(event):
    coords = (str(event.x) + " " + str(event.y))
    print (coords)

canvas.bind("<Button-1>", get_coords)
 
def open_file():
    file = askopenfile(mode='r', filetypes=[('Video Files', ["*.mp4"])])
    
    if file is not None:
        global filename
        filename = file.name
        global videoplayer
        videoplayer = TkinterVideo(canvas, scaled=False)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(side=LEFT, expand=True, fill="both")
        videoplayer.play()
 
 
 
def playAgain():
    print(filename)
    videoplayer.play()
 
def StopVideo():
    print(filename)
    videoplayer.stop()
 
def PauseVideo():
    print(filename)
    videoplayer.pause()



#lbl1 = Label(window, text="Tkinter Video Player", bg="orange red", fg="white", font="none 24 bold")
#lbl1.config(anchor=CENTER)
#lbl1.pack()
 
openbtn = Button(window, text='Open', command=lambda: open_file())
openbtn.pack(side=BOTTOM, pady=2)
 
playbtn = Button(window, text='Play Video', command=lambda: playAgain())
playbtn.pack(side=BOTTOM, pady=3)
 
stopbtn = Button(window, text='Stop Video', command=lambda: StopVideo())
stopbtn.pack(side=BOTTOM, padx=4)
 
pausebtn = Button(window, text='Pause Video', command=lambda: PauseVideo())
pausebtn.pack(side=BOTTOM, padx=5)


def on_closing():
    global closed
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        closed = 1
        window.destroy()

window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()
Thanks
Because you are clicking on the video player, not the canvas. You could bind button events in the video player
videoplayer = TkinterVideo(canvas, scaled=False)
videoplayer.bind("<Button-1>", get_coords)
Or you could bind button events in the root window. Binding button events in the canvas is probably useless.