Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lost Control over VLC
#1
First post; not sure if this is the proper forum?
I'm working on a Python Windows VLC media player script, and ran into a problem I can't figure out.
I've done a lot of reading and testing, but am getting nowhere.

After I start playing my video I need a popup window to select the proper subtitle.
As soon as I select the popup, I lose control of VLC.
I can close the popup and the video keeps playing but I have no control over it.

I have included the 2 functions that are relevant. They have been stripped to the minimum required to produce the problem.
I would truly appreciate any help.
Thanx!

from pynput import keyboard
from pynput.keyboard import Key
import time
import keyboard
global media_player

def vlc_player(filename):
       # creating vlc media player object
       media_player = vlc.MediaPlayer()
       # making keyboard input enable
       media_player.video_set_key_input(True)
       # media object
       media = vlc.Media(filename)
       media_player.set_media(media)
       # toggling full screen
       media_player.toggle_fullscreen()
       # set volumes
       media_player.audio_set_volume(100)
       #time=media_player.get_time()
       media_player.play()
       # STOP playing when end-of-file reached
       Ended = 6#set arbitrary value
       current_state = media_player.get_state()
       while current_state != Ended:
               # define keys pressed during playback
               if keyboard.is_pressed("s"):
                       media_player.stop()
               if keyboard.is_pressed("ctrl+x"):
                    sub_popup()
               current_state = media_player.get_state()
       media_player.stop()


def sub_popup():
   
       top = Toplevel(my_canvas, width = 250, height = 400)
       top.geometry('%dx%d+%d+%d' % (300, 350, 800, 200))
       top.configure(bg="black",highlightthickness=7)
       top.wm_overrideredirect(True) 
       top.attributes("-topmost", True)
       play = Button(top, text="Okay", command = top.destroy)
       play.pack()
       top.mainloop()
deanhystad write Jul-18-2023, 03:01 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
mainloop() blocks your program from running until the last window is destroyed. You should not put mainloop() in your sub_popup() function if there is a mainloop() elsewhere in your program (There can only be one).
Reply
#3
(Jul-18-2023, 03:20 PM)deanhystad Wrote: mainloop() blocks your program from running until the last window is destroyed. You should not put mainloop() in your sub_popup() function if there is a mainloop() elsewhere in your program (There can only be one).

Thanx for your response.
When I remove the mainloop the popup doesn't display at all.

The mainloop statement is not in my full script, but I still have the same problem.
I will try to upload the script.

Attached Files

.py   Main-Script.py (Size: 39.1 KB / Downloads: 35)
Reply
#4
Is that the only time you call mainloop() in your program? Is that the only tkinter window? What is my_canvas?
Reply
#5
(Jul-18-2023, 05:07 PM)deanhystad Wrote: Is that the only time you call mainloop() in your program? Is that the only tkinter window? What is my_canvas?

I only call mainloop at the end of the script.
There are several tkinter windows.
I'm trying to send you the full script, but having a problem.

OK, maybe it's there now?
Reply
#6
It just dawns on me that you may need my SQL.DB to test the code.
Currently it's around 14MB, but I could probably delete a lot of the records if you need it.
What is the maximum size I can attach?

I just deleted about half of the records and the size is still 14MB.
Reply
#7
Have you written tkinter programs before? They have a pattern that you need to follow, a pattern very different than a script.
Reply
#8
You have a problem with the design of your program. tkinter.mainloop() blocks other code from running. You also have a loop that looks for key presses that blocks other code from running. Somehow you need to get these to work together.

I think I would move this loop:
       Ended = 6#set arbitrary value
       current_state = media_player.get_state()
       while current_state != Ended:
            ...
To a function that I would call periodically using root.after(milliseconds, func_to_call). Another idea is use event binding to direct key presses to some function. You are already doing this with some key presses, but not all. And there is always multi-threading. Using multi-threading you could set up a keyboard listener that runs in one thread and have tkinter run in another thread. Getting the two threads to work together can be tricky.
Reply
#9
(Jul-18-2023, 05:53 PM)deanhystad Wrote: I'm pretty sure you can make a test case that does not require a database. If you could just add the code that sets up the keyboard listener and creates the root window (if there is one), that would go a long way toward making something others can run.

Hopefully you got the full script I attached?
I am a complete novice with Python and Tkinter, and have been teaching myself thru reading and trial and error.
My program has come a long way, but I'm stuck now with this one problem.
I just can't resume my VLC session after calling the popup.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Lost Modules standenman 2 739 Jun-22-2023, 12:18 PM
Last Post: standenman
  Lost Module standenman 10 2,876 Oct-30-2021, 05:11 PM
Last Post: deanhystad
  XML Editing formatting lost ateestructural 2 1,926 Apr-08-2021, 04:41 AM
Last Post: ndc85430
  List structure lost when multiplying Protonn 2 2,258 Apr-23-2020, 04:16 AM
Last Post: buran
  lost dictionary jjpy 1 1,900 Jul-10-2019, 12:19 PM
Last Post: scidam
  variable gets lost Jordy 10 9,162 Oct-10-2016, 08:08 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020