Today, I’ll explain a relatively new GUI framework with which you can create native mobile applications across all the different platforms. Note that this framework is still in the preview phase on many fronts. And you also can contribute here in many ways.
Let’s jump into making a chat application using this. It is relatively easy to build.
You need to install the following framework –
pip install beeware==0.3.0.dev3
As I’ve told you that this package is in the preview stage. So, you need to wait for few more days to get this package available for production use.
However, the following diagram presented in Pycon explains that all –
Let’s jump into our objective.
To create the project, the following are steps that need to perform –
Functions that you need to use –
This will lead to several options that you need to fill as shown in the next couple of slides –
Note that, this one I’ve created in the Windows environment. So, you need to provide all these details before creating the app.
This will create a template of code as shown below –
And, you can run this template to see the default template output by using the following command –
Now, I want to take this & add some lines of codes to create a chat-based application in MAC & see how it behaves.
But, before that, the directory structure will look like this –
As you can see, SDChat is my application name. And, based on that, the following directories.
And, inside the final SDChat directory, the following files are created –
Let’s take this & modify it for MAC. To do that, we need to change app.py script & layout our all the essential GUI ingredients.
Note that I’m not going to discuss the custom bot that I created. Only, I’ll be referring to it.
1. app.py (This script contains all the GUI details & it will invoke the custom bot.)
Let’s discuss a couple of essential lines from the above snippet –
self.chat = toga.DetailedList(data=, style=Pack(flex=1)) self.chat.data.append(icon = toga.Icon('resources/brutus.png'), title='SDChat', subtitle='Hi! How are you doing today?')
This is the main display box, where you can see all the chat details. And, also, by default, it will prompt the initial conversion starter.
self.text_input = toga.TextInput(style=Pack(flex=1))
This is the place where you will be typing your text.
send_button = toga.Button( 'Send', on_press=self.greet, style=Pack(padding_right=5) )
Here is the button that you are creating. As you can see, almost all the places I’ve provided the “Style,” which is key to your object alignment inside your mobile app.
main_box = toga.Box(children=[self.chat, input_box], style=Pack(direction=COLUMN)) self.main_window = toga.MainWindow(title=self.formal_name) self.main_window.content = main_box self.main_window.show()
Finally, you need to bind all the components & you will show the page.
Now, let’s discuss the function that I’ve created for my button –
def greet(self, widget): input_text = self.text_input.value self.chat.data.append(icon=toga.Icon('resources/user.png'), title='You', subtitle = input_text) # Chatbot y = e.clsTalk2Bot() ret_val = y.TalkNow(input_text) self.chat.data.append(icon=toga.Icon('resources/brutus.png'), title='SDChat', subtitle=ret_val) self.text_input.value = '' self.chat.scroll_to_bottom()
As you can see, here, our application will capture the user input & based on that, our program will pass the input text to our chatbot. Also, you can see once that communication & response achieved, the input box will be cleared & the control will move down to the end of the chat screen. This is required. Otherwise, the user won’t be able to view the latest communication.
So, as you can see that, this extremely easy to create & you can enhance it as per your need.
For more information on this framework, please go through the following link ->
For more details, view the main page ->
I’ll be posting another new post in the coming days. Till then, Happy Avenging! 😀
Note: All the data posted here are representational data & available over the internet & for educational purpose only.