appJar
The easiest way to create GUIs in Python.
Written by a teacher, in the classroom, for students.
appJar is designed to run on as many versions of Python as possible - so it should work in your school.
There are no other dependencies - simply download, unzip, and put it in your code folder.
Check out the installation instructions for other ways to get appJar working.
GUIs in Python are hard, there's a huge amount of boilerplate code required to get things working - so we hide all of that.
We're also not big fans of lots of parameters, so we keep them to a minimum, instead giving functions to get & set most things.
Hello appJar
GUIs in appJar require three steps.
- First, import the library & create a GUI variable.
(from now on, we do everything to the GUI variable)
# import the library
from appJar import gui
# create a GUI variable called app
app = gui()
- Then, using the gui variable, add and configure some widgets:
(if you've tried turtle this will all look very familiar)
# add & configure widgets - widgets get a name, to help referencing them later
app.addLabel("title", "Welcome to appJar")
app.setLabelBg("title", "red")
- Finally, start the GUI:
(NB. don't put any code after this line)
# start the GUI
app.go()
-
And, that's it:
Interactivity
Of course, the whole point of making a GUI, is to be interactive - this requires events.
The idea behind event-driven programming is that each time the user clicks, presses or drags something (an event) the GUI should respond.
- First, add some more widgets (Entry Boxes), for the user to interact with:
app.addLabelEntry("Username")
app.addLabelSecretEntry("Password")
- Then, we'll need a function - a block of code to call, when an event happens:
def press(button):
if button == "Cancel":
app.stop()
else:
usr = app.getEntry("Username")
pwd = app.getEntry("Password")
print("User:", usr, "Pass:", pwd)
- Finally, some widgets (buttons) to create the events:
# link the buttons to the function called press
app.addButtons(["Submit", "Cancel"], press)
-
We now have an interactive GUI:
When the user presses a button, the press
function is called, passing the name of the button as a parameter.
Appearance counts
As well as changing widgets, you can also change the way the GUI looks:
- For starters, you can specify a name and size for your GUI, when you create the variable:
app = gui("Login Window", "400x200")
- As well as changing widget appearance, you can also change the general GUI's appearance:
app.setBg("orange")
app.setFont(18)
- You can even specify where you want the cursor to be when the GUI starts:
app.setFocus("Username")
-
It now looks a bit better:
(NB. We also set some other colours on the label - see below)
Make your own
And, that's all you need to know. Check out:
- All the different widgets available.
- Our support for images and sound.
- How to include toolbars, menubars & statusbars.
- How to create simple pop-ups.
- How to use a grid layout.
- How to use containers for more advanced layouts.
Full code-listing
Below is the full code-listing created on this page:
# import the library
from appJar import gui
# handle button events
def press(button):
if button == "Cancel":
app.stop()
else:
usr = app.getEntry("Username")
pwd = app.getEntry("Password")
print("User:", usr, "Pass:", pwd)
# create a GUI variable called app
app = gui("Login Window", "400x200")
app.setBg("orange")
app.setFont(18)
# add & configure widgets - widgets get a name, to help referencing them later
app.addLabel("title", "Welcome to appJar")
app.setLabelBg("title", "blue")
app.setLabelFg("title", "orange")
app.addLabelEntry("Username")
app.addLabelSecretEntry("Password")
# link the buttons to the function called press
app.addButtons(["Submit", "Cancel"], press)
app.setFocus("Username")
# start the GUI
app.go()