I am hoping that someone can help with a Python bindings output question (using vlc.py)
I have a basic test script that uses vlc.py which runs but does not play the video.
import vlc
def setup_player(filename):
vlc_instance = vlc.Instance('--no-audio', '--fullscreen')
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(filename)
player.set_media(media)
print media.get_mrl() # File location to get title
print player.get_length() #Time duration of file -1 means there is no media
print player.get_state() #Player's state
player.play()
setup_player('foo.mp4')
This outputs
file:///Users/admin/Sites/pythontest/foo.mp4
-1
State.NothingSpecial
I am unsure where to install the vlc.py module and hoping someone can help. I'm on MacOs, VLC 2.0.9, Python 2.7.3. Running python through the terminal.
At the moment I have the vlc.py module in the same directory as my test script - and outside of the VLC.app directories and although the script is executing without errors it isn't playing the video or returning any parameters about the specified mp4 file.
Apologies for a banal question! Any help very gratefully received.
It seems that the player.play() function is not blocking, but instead returns immediately.
If the Python script then terminates, the player is destroyed right after it has been created.
If you look at the example player in vlc.py, it has a while True loop at the very end that basically reads key presses over and over again in order to implement a simple user interface.
So if you simply add
while True:
pass
at the end of your function, it should continue playing (terminate with CTRL+C until you implement some sort of user input handling).
As for "installing" the script: Unfortunately, the vlc.py module they provide is just that, a simple stand-alone Python module. It's not packaged as a setuptools distribution that you could just install with pip or easy_install like most other Python modules. That means you can (or rather have to) drop it into a location that will be in sys.path yourself.
The current working directory where you launch your script from works for that, but if you want a more permanent location you could drop it into your Python's site-packages (/Users/<your-username>/Library/Python/2.7/lib/python/site-packages for example if you're using the standard OS X framework Python).
Since the API calls succeed, it means that the bindings work correctly. The problem here is rather that the macos x video output module is not able to instanciate its own window. It must be embedded in a native Cocoa widget.
You can use the qt example from the vlc repository or use the x11 video output module, combined with the MacOS X X11 server (which has to be installed for recent versions of MacOS X).
Related
I'm working on a project with wxPython, which includes a non-standard font. On Windows there's no problem, as I can just use wx.AddPrivateFont, however, macOS (OS X) does not support this, and the font has to be already installed. I've found that this can be done by placing the file in the users Fonts directory like this: (I'm of course making sure that this will only run under OS X)
user_font_directory = f"/Users/{getpass.getuser()}/Library/Fonts"
shutil.copyfile(path, f"{user_font_directory}/{file_name}")
This actually works, and any program that runs after this, is able to use the font. However, the program that installed it is itself not able to use it before it is restarted manually, as there seems to be some sort of font cache that is only given to a process right when it's launched. To get around this i firstly tried just reinitializing the wx.App, but this made no difference. I then tried making Python restart itself using this method:
def restart_program():
python = sys.executable
os.execl(python, python, *sys.argv)
But still to no avail. I then tried separating the installation itself into another script, which after completion launched the main script as a sub process:
subprocess.run([sys.executable, "main.py"])
This had no effect.
So is there a way to make Python programmatically reload the font cache from OS X (or is there a better solution altogether)?
I'm asking help today because I'm new to Tkinter and Pyinstaller (and python in general) and I'm having troubles with it.
I have a simple app working with sqlite, tkinter and pyinstaller to compile all of this in an executable program, the entrance point of my program is a file named main.py
This file calls all the dependancies (like the sqlite module for python, tkinter and my other files like classes etc...)
I made a very simple interface, with a Hello World in a tkinter label and a button to go to page 2 which displays page2 (also in a label), just to see if I'm capable of making it all run and compile all of these pieces together.
I can run it throught my shell executing it like : python main.py and everything is working fine.
But when I run pyinstaller on my linux machine, and start executing the program, nothing appears, my database.db (sqlite database file) is created but I don't have any interface like when I run it with my shell. The thing is getting even worse on windows where, once I've my .exe it just opens a shell and crash after few seconds, not even creating the database.
What I did is I created a 'log file', in which I write the steps of the program.
As you can see on the following picture, the 2 first prints are wrote in my log file (on linux), so I think it crashes when I try to create the window.
If any of you have an idea on what I do wrong, I would really appreciate help :)
General
From the PyInstaller manual:
Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder. It is is much easier to diagnose problems in one-folder mode.
As the comments suggested, use a catch-all try/except block to log all exceptions to a file. That is probably the best way to see what is really happening. Make sure that the logfile is created in an existing location where you have the necessary permissions.
I would suggest to take advantage of the built-in logging module instead of creating your own. It can e.g. automatically add from which file a log line was created.
IMHO, it is probable that the failures on Linux and ms-windows have completely different causes. You should probably treat them as different issues.
Linux
When you use single file mode, that file is unpacked into a temporary folder, probably somewhere in /tmp. Some Linux distributions mount the /tmp filesystem with the noexec flag. This is incompatible with PyInstaller.
ms-windows
On windows, there are basically two different Pythons; python.exe and pythonw.exe. Basically it is one of the quirks of windows that this is necessary. The latter is for GUI programs like tkinter programs. A tkinter script should not show a cmd window. So I'm guessing that PyInstaller calls your command with python.exe instead of pythonw.exe. From the manual:
By default the bootloader creates a command-line console (a terminal window in GNU/Linux and Mac OS, a command window in Windows). It gives this window to the Python interpreter for its standard input and output. Your script’s use of print and input() are directed here. Error messages from Python and default logging output also appear in the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide a console window. The bootloader starts Python with no target for standard output or input. Do this when your script has a graphical interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file extention of .pyw suppresses the console window that normally appears. Likewise, a console window will not be provided when using a myscript.pyw script with PyInstaller.
Also, on windows it can matter which Python distribution you're using. I used to be a fan of Anaconda, but lately I've come to prefer the python.org version because it gives me less headaches. On anaconda Python I had the problem that tkinter programs would not launch without showing a cmd window, whatever I tried. Only switching to python.org Python solved that problem.
I have written a python game using pygame module. It runs fine from terminal.
I have a game directory (let's call it myGame) which contain all the resources(images and sounds) and all the python scripts which use those.
When I run the game through terminal (let's say python3 mainPythonModule.py), the game runs fine.
I tried to create a game.desktop file. But when I click it, a black window appears and crashes almost instantly.
I am using Ubuntu 16.04 LTS. And my game is written in python3.5 and uses pygame module.
My game.desktop file looks like this
I don't want the solution answered for this question because I still have to type in terminal.
I would like to create a desktop applcation, such that by clicking on the icon, the game would start.
Just like when we download any game, after installation, we just click on the game icon and it runs, I want something like that. How can I do that?
Edit :
I don't think this question is a possible duplicate of this question.
I want to know how to develop a desktop application in Ubuntu 16.04, such that by clicking on some icon, the game would start.
Final Edit :
First of all, I would like to thank everyone who responded. I really am sorry for wasting your precious time.
The issue is resolved. The problem was all the resource (images and sounds) were loaded via relative path and not through absolute path. Now the game.desktop file runs fine.
I think what are you trying to do is some kind of launcher.
Here you have my repo, it contains a Tkinter GUI with a PyGame call on a button.
Feel free to study/use it as you want, look at the file desktop_application.py and if you don`t care about the coin part you can just skip that and take what you need.
https://github.com/FilippoLeone/PyGameLauncher
And you can compile everything as an executable with PyInstaller.
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
Say I have a folder called "Family Photos" and I want to automatically run a python program if that folder is selected. How would I go about doing that? Would I just put the code in the folder and it runs automatically?
Edit: I'm on Windows 10
You can use tkinter in python.
Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one
You can use other GuiProgramming toolkits as well. Follow this_link to know about other gui frameworks.
For how part in Tkinter .Follow this_link
Some unix/linux systems are open source so you could modify the OS behavior to do that, i don't believe windows offer this feature, you probably should create an APP for that
The best solution would be a python scripts that run it self each hour and check if this folder is modified and do something if its, and it will run each time you turn on the computer once
I'm using pygame on a PC (PC-104) with ubuntu server, for this reason it only has terminal, not UI.
On my laptop the code works perfecly, running the code on the terminal "eg: python game.py", but on the PC104 it gives "Video system not initialized", I read the error is shown because it is a way to use events on a system without UI?
Thanks.
Thank for your answer jsbueno. I was able to found a solution a few weeks ago but forget to answer the question.
I wasn't able to run any pygame script with any other library than X11. But I found that one can run the script with no graphics library. Just setting the SDL_VIDEOLIBRARY enviroment variable to "dummy".
This is posible on bash but I prefer to do it on python:
os.environ["SDL_VIDEODRIVER"] = "dummy"
With this I was able to run the pygame script, detect joystick events, etc.
It is possible to run pygame programs in a system without X11 if you set it to use framebuffer or vgalib - the docs even talk about using aalib (which would display graphics using ascii art on the terminal.)
This part of the documentation has it:
Pygame will select from one of several internal display backends when
it is initialized. The display mode will be chosen depending on the
platform and permissions of current user. Before the display module is
initialized the environment variable SDL_VIDEODRIVER can be set to
control which backend is used. The systems with multiple choices are
listed here.
Windows : windib, directx
Unix : x11, dga, fbcon, directfb,
ggi, vgl, svgalib, aalib
So, what you have to do is set the SDL_VIDEODRIVER environment variable before starting your code. And being shure the proper lib. is installed.
For more information:
http://www.pygame.org/docs/ref/display.html