Python Graphics Quickstart |
As you might expect, there are TONS of ways to do graphics in Python; there are ports that make accessible about every sort of graphics framework ever available. For many Python projects, however, what you need is just a very simple way to display some information, e.g., a Boggle board, a Battleship board, or other boardgame board. You could invest tons of time and make these look at a nice and pretty as any GUI you've ever seen...but in many cases, we'll just want to get something up that looks halfway decent and does the job. Here are a couple of options:
Nearly every language has some basic GUI layout framework included. One of the earliest and best developed of these was TK, which has been around since the late 80's, the very start of GUI history. Of course it has been developed extensively since then, but the basics remain: a simple way to render various standard GUI "widgets", like buttons, scroll bars, etc., plus a layout framework for placing them on the screen. If you want interactivity (e.g. buttons that actually work), you then wire these GUI elements to code, essentially implementing their stubbed "on click" method, for instance.
If you've programming in Java, then you might be familiar with Swing, which is Java's built-in GUI framework. Swing is directly based on TK; you might even consider it a Java wrapper for TK. Python also has its built-in basic GUI package called Tkinter. This allows you to open windows, define layouts, and populate them with widgets. Here's a nice little sample GUI article; there are a bazillion other tutorials too. This a nice compact tkinter reference that I found useful.
The GUIs that you make with tkinter are not particularly pretty...it's a fairly quick and dirty approach. If you wanted to upgrade to a bit more sophistication, you could look at GTK+ or one of the other GUI toolkits for Python. But tkinter would get the job done for sure. For instance, the image at the right is a nifty basic Halma board I whipped up in a couple of hours, starting from knowing zero about tkinter, just to mess around with what can be done and how it might look. Not bad at all. And I discovered that's it's a piece of cake to actually do cool interactive GUI stuff like allow you to move pieces by clicking. tkinter is a pretty slick and fast package!
We're all for re-use when we can, and one of the simplest yet best developed GUI environments of all is the modern web browser. So you could craft an alternate plan: Have your python program launch a very simple HTTP server, then serve up your graphical representation of the game board in HTML, e.g., as an HTML table. Obviously, a table is (a) a very well-supported 2D grid layout in HTML and (b) perfect for square gameboards with cells. And starting a simple HTTP server in Python is a one-liner, if you utilize the SimpleHTTPServer package.
The plus here would be the nice support for coloring (backgrounds for tables/row/cells) and the many other cool things one can do with tables...and that's not even thinking about the extra fancy stuff one could build in by adding CSS. The downside is interactivity: how will your HTML board know to refresh when pieces are moved. And how will you handle displaying status messages and getting input from that interface. You could, of course, just have the HTML board be "display only", and do your status and input fields somehow back in the Python window...but that would be clunkly. There are plenty of ways to handle the interactivity in HTML (Forms, Javascript) that displays data from the server side, and brings input back to the server side.
The two options above are just what comes to mind immediately when thinking about a simple way to handle a GUI in Python, particularly when the real focus of the project is elsewhere, e.g., in actually getting your computer to play a game that you're programming. As I've indicated, there are tons of other GUI packages that have been ported to Python, and you'll almost certainly want to explore these if you have a more "free-form" interface, e.g., where you are just drawing game elements into a graphics port, rather than having standard windows, buttons, checkboxes, and other standard GUI elements. One very simple option in this direction is to use the super easy and effective Turtle Graphics package in Python. This is perfect if you are displaying anything that can be drawn with a series of pen strokes (filled or unfilled).