简体   繁体   中英

How do I start a Python screensaver that takes keyboard input in Ubuntu (and preferably Windows)?

I am developing a small application for home use in Python. It is supposed to act as a slide show screensaver, but also as a primitive image manager. I have the slide show and image manager aspects covered (I am using Tkinter), but I haven't implemented the screensaver bit yet. So, starting the app from the command line works fine.

I am running Ubuntu and as I need the application to be able to accept keyboard input I do not think I can use the xscreensaver/gnome-screensaver framework as it captures everything and displays the login dialog instead. I'd love to be proven wrong here (and it would be nice to return to xscreensaver after so many years :) ).

The solution I have in mind is to use PyXSS to detect when the user is idle and manage the screensaver bit myself. But it would be ideal to find another, platform independent solution, since I'd like to share this program with some of my Windows-using friends.

On to my questions, then:

  1. Can I use xscreensaver in some way to start my program but avoid xscreensaver's keyboard handling?

  2. Are there platform-independent alternatives to PyXSS?

  3. How would I go about to achieve the same functionality in Windows?

For Windows , you can just create an executeable (eg. with py2exe ), and rename it from .exe to .scr

You can right-click on that file and select "Install" or just copy it into your(?) Windows/System32 directory for it to show up in the list of screensavers!

One assumption though: I think it would be best to create just one executable instead of the regular bunch of files (=dependencies).

I use this setup-configuration (for py2exe ) to create one single executable:

from distutils.core import setup
import py2exe

includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter','MSVCP90.dll']
packages = []
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
                'tk84.dll']

setup(
    options = {"py2exe": {"compressed": 2,
                          "optimize": 2,
                          "includes": includes,
                          "excludes": excludes,
                          "packages": packages,
                          "dll_excludes": dll_excludes,
                          "bundle_files": 1,
                          "dist_dir": "dist",
                          "xref": False,
                          "skip_archive": False,
                          "ascii": False,
                          "custom_boot_script": '',
                         }
              },
    zipfile = None,
    windows=['main.py'] # <- the name of your code file
)

Edit: For it to compile you will need the MSVCP90.dll file in the same folder. If you can't find it on your PC, then just use google to find it!

Commandline Operators: There are several commandline operators handed over, depending on what is happening (Preview, Settings, etc.). You would have a list here: http://www.wikihow.com/Convert-an-Executable-File-Into-a-Screensaver

Sadly what's written there is not all correct, so in short:

  • /s and /S ... when the screensaver starts (one of them when previewed)
  • /c:# ... when the configure-button is pressed. It might be best to just use the first two chars if sys.argv[1][:2]=='/c': works well for example.
  • /p # ... is called a number of times (when you close the configuration, or after preview, etc...), everytime it returns to the general screensaver settings. I just discard this along with any other possibilities (other than /s /S or /c:#). Works fine!

User Input: Handling keyboard-strokes is really easy, because it will not automatically quit on mouse-moves, or keyboard events, but you will have to implement these methods yourself! So don't forget to write your "on_mouse_event_close_program" function!

Ummm, well there's pyhack which allows you to use xscreensaver which is available for Mac and Linux. Not sure about the pyhack module, but xscreensaverhack allows you to capture keyboard and mouse events.

I heard that there might be a python library for Windows screensaver programming. Even if there isn't, you can always use the win32api that comes with the win32 python package. Tutorials on the Internet will tell you which api functions to call for screensavers. Not sure about using pyopengl or anything like that....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM