[Python] Desktop background changer

Discussion in 'Programming General' started by Lobe, Oct 16, 2008.

[Python] Desktop background changer
  1. Unread #1 - Oct 16, 2008 at 7:32 PM
  2. Lobe
    Referrals:
    0

    Lobe Guest

    [Python] Desktop background changer

    Seeing as this has no other place (Unless it goes in web programming, which I doubt) I will post it here.

    This python script changes the desktop background at random, from a folder of images. Wait until it loads, then press F9 to cycle through all the backgrounds in the folder, or F8 to choose another random one. F7 closes the program.
    Download some nice wallpapers from The deviant art wallpaper section, and place it in C:/backgrounds/

    It demonstrates three things:
    • How to hook global windows hotkeys
    • An introduction to the windows API
    • How to use glob to find a list of files

    Requires:
    Python 2.5.2 <- NOT 2.6+!
    WIn32 extensions
    Psyco - Not necessary, but improves speed. Remove the import and psyco.full() bit to remove requirement
    Ctypes (May come with python 2.5)

    The code is a bit untidy:
    Code:
    # Random Desktop Wallpaper Chooser
    print 'Started'
    import ctypes, win32con, glob, random, shelve
    from ctypes import wintypes
    import psyco
    psyco.full()
    print 'Imported modules'
    byref = ctypes.byref
    user32 = ctypes.windll.user32
    HOTKEYS = {
      1 : (120,0),
      2 : (118,0),
      3 : (119,0)
    **
    def handle_win_f3(): do_list()
    def handle_win_f4(): exit()
    def handle_win_f5(): random_background()
    counter = 0
    def do_list():
        global counter
        if counter == (len(files)-1):
            print 'COUNTER RESET'
            counter = 0
            set_background(files[counter])
        else:
            counter = counter + 1
            set_background(files[counter])
        print "%s/%s"%(counter,len(files)-1)
    failed = False
    for id, (vk, modifiers) in HOTKEYS.items ():
        if not user32.RegisterHotKey (None, id, modifiers, vk):
            print "Unable to register id", id
            failed = True
    if failed: print 'Not all hotkeys may work'
    print 'Hooked keys'
    lasta = None
    
    g = shelve.open('C://backgrounds//files//wallpaper_db')
    if not g.has_key('last_wp'):
        g['last_wp'] = 'None'
    print 'Opened the shelve'
    def set_background(path):
        print path
        cs = ctypes.c_buffer(path)
        b = ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_SETDESKWALLPAPER,0, cs, win32con.SPIF_SENDWININICHANGE)
    HOTKEY_ACTIONS = {
        1 : handle_win_f3,
        2 : handle_win_f4,
        3 : handle_win_f5
    **
    path = 'C:\\backgrounds\\*.*'
    files = glob.glob(path)
    def random_background():
        if len(files) == 0:
            print 'No files detected in C:\backgrounds!'
            raw_input('exiting when you press enter')
            exit()
        if g['last_wp'] in files:
            for index in range(len(files)):
                if files[index] == g['last_wp']:
                    #print 'deleted %s'%files[index]
                    del files[index]
                    break
        choice = random.choice(files)
        cs = ctypes.c_buffer(choice)
        b = ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_SETDESKWALLPAPER,0, cs, win32con.SPIF_SENDWININICHANGE)
        g['last_wp'] = choice
    random_background()
    print 'Random background chosen'
    print 'F9 - Cycle through backgrounds'
    print 'F8 - Choose another random background'
    print 'F7 - Quit'
    
    
    try:
      msg = wintypes.MSG ()
      while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
        if msg.message == win32con.WM_HOTKEY:
            action_to_take = HOTKEY_ACTIONS.get(msg.wParam)
        if action_to_take:
            action_to_take()
        user32.TranslateMessage (byref (msg))
        user32.DispatchMessageA (byref (msg))
    finally:
      for id in HOTKEYS.keys ():
        user32.UnregisterHotKey (None, id)
    g.close()
    
     
< Macro functions and procedures | An idea for someone to make >

Users viewing this thread
1 guest


 
 
Adblock breaks this site