GUI using AppJar in Python
Welcome to this tutorial on GUI using AppJar in Python using Login App. Python supports quite a few APIs to work on GUIs. Some of the more famous ones are AppJar, Tkinter, WxPython, etc. In this tutorial, we will talk about AppJar.
Simple login form using AppJar in Python
The best way to learn something is by doing. So we will make a small project – a simple user login form – and while building this project, the steps will be explained step by step, right from the basics.
So fire up your computers and let’s begin coding.
Installing AppJar is very easy. Simply open Command Prompt by typing ‘cmd’ in your Start App and copy the following line.
pip install appjar
You can also download the file from the link, unzip it and put it in your code folder.
To use the methods and functions of AppJar we will need to import it inside our program called main.py.
from appJar import gui
Create a Class for the App – AppJar
In order to make the code easier and legible, we will form a separate class for the Application GUI interface. This function will hold the different methods that we will need to make the interface functional. Here we name the class as MyApplication.
Make App Window in AppJar
The very first method for the class will be to create a window for the app. The different elements of the window are called Widgets. In order to make the window, certain widgets are added to the window such as the title of the window, set the Font of the text, what happens on clicking the Cancel button, etc.
def CreateAppBox(self, app): app.setTitle("Login Form") app.setFont(16) app.setStopFunction(self.BeforeExit) self.AddWidgets(app) return app
Now inside the window, we will have two labels one saying username and the other password. Next to these labels we will add the text boxes for user input. For Password entry since we do not want everyone to see what is being typed in, the addSecretEntry() method is being used.
Finally, the submit and cancel buttons are added at the bottom.
def AddWidgets (self, app): app.addLabel("userLab", "Username:", 0, 0) app.addEntry("username", 0, 1) app.addLabel("passLab", "Password:", 1, 0) app.addSecretEntry("password", 1, 1) app.addButtons(["Submit", "Cancel"], self.Submit, colspan=2)
Prepare the app
This particular function starts the app when it is called from the main menu. The app.go() function is comparable to the Thread.run() method that runs the app.
def Start(self): app = gui() app = self.Prepare(app) self.app = app app.go()
One of the trickiest part of making the app is what happens when the cancel button is clicked. But thankfully AppJar provides a yesNoBox()
to set a prompt and makes things very simple for us.
def BeforeExit(self): return self.app.yesNoBox("Confirm Exit", "Are you sure you want to exit the application?")
Event Listener for Submit
Next, we need to decide whether the entries made in the text boxes are valid credentials or not. In order to access the user input, the getEntry() method is used. Once we get the texts, a simple if else condition to check the credentials should suffice for this particular example.
Upon valid credentials, a Logged in prompt is shown. Whereas on invalid credentials an error message is shown.
def Submit(self, btnName): if btnName == "Submit": username = self.app.getEntry("username") password = self.app.getEntry("password") if username and password == "PythonAppJar": self.app.infoBox("Logged in", "You are now logged in !") else: self.app.errorBox("Error", "Your credentials are invalid !")
This is the driver menu that creates an object of the Application class. The App.start() method is again comparable to the Thread.start() method where it wakes up the App. From here when we call the App.go() meth0d in the Start() methid of the class MyApplication, the App starts to run after being woken up.
if __name__ == '__main__': App = MyApplication() App.Start()
The image attached shows the result of the above code.
The Documentation for the AppJar library is being provided below. Feel free to check that out and play around with the different functionalities.
And now that you have the tools, get started on making the app that you always wanted to.
For more such articles: Create A Discord Webhook in Python for a bot