Steam sales are a wonderful thing and with the Autumn Sale going on now, a lot of game libraries are going to grow larger by the end of it. For this reason, I thought it might be a good idea to make a random Steam game chooser to give some of those dusty, forgotten games in the games list a chance to be played.


First, you’re going to need a Steam API key which you can get from here. You’ll also need to know your 64-bit Steam ID which you can find from here. Make sure you have the Pillow (PIL) library too. The code is fairly straightforward:

import json
import random
import webbrowser
from urllib.request import urlopen
from urllib.error import HTTPError

from tkinter import Tk, Button
from PIL import ImageTk

apikey = ''
steamid = ''

def getownedgames(apikey, steamid):
    url = (''
           '?key={}&steamid={}&include_appinfo=1'.format(apikey, steamid))

    return json.loads(urlopen(url).read().decode())['response']['games']

def getimage(game):
    imageurl = '{}/header.jpg'
    data = urlopen(imageurl.format(game['appid'])).read()

    return ImageTk.PhotoImage(data=data)

def playgame(game):'steam://rungameid/{}'.format(game['appid']))

def choosegame(games, tk, button):
    game = random.choice(games)

        game['image'] = getimage(game)
    except HTTPError:
        return choosegame(games, tk, button)

    button.configure(image=game['image'], command=lambda: playgame(game))


games = getownedgames(apikey, steamid)

tk = Tk()
tk.resizable(0, 0)

gamebutton = Button(tk, width=460, height=215, bd=0,
                    bg='gray11', activebackground='gray11',
                    relief='flat', cursor='hand2')

changebutton = Button(tk, width=41, height=1, bd=0,
                      fg='white', activeforeground='white',
                      bg='gray11', activebackground='gray11',
                      relief='flat', cursor='hand2',
                      font=('Segoe UI Semilight',), text='Nope!',
                      command=lambda: choosegame(games, tk, gamebutton))




The code sets up a Tk window with an image of the game acting as a link to play it and a button underneath to find another random game. The getownedgames function gets a list of the user’s Steam games as a JSON object which is converted to a dictionary using the json.loads function from the json module. To open the game, the webbrowser module is used to open the Steam URL of the game in the default handler.

Sometimes a game doesn’t have an image which is why I have the try-except block in the choosegame function. Also, the image is stored in the game dictionary as we need to maintain a reference to any Tk images.

For the GUI, to make the window non-resizable, tk.resizable(0, 0) is called. Each of the buttons is passed a function for the command argument which is called when the button is pressed. Finally, the choosegame function is called by invoking it through the second button.

Save the file with a .pyw extension to not show a console window.