Interface and Application Programming
Assignments
All the important links are Here
Learning outcomes
Vocabulary
Interface
An interface generally refers to a point of interaction or communication between different entities, systems, or components. In computing, an interface often refers to a boundary or connection point between different software components, hardware devices, or systems that allows them to communicate or interact with each other.
There are several types of interfaces in computing:
- User Interface (UI): This is the interface through which users interact with software applications or systems. It includes elements such as graphical user interfaces (GUIs), command-line interfaces (CLIs), and voice-based interfaces.
- Application Programming Interface (API): An API defines the methods and protocols through which software components or systems can interact with each other. It specifies how software modules should interact and exposes functionalities that other software can use.
- Hardware Interface: This refers to the connection point between hardware devices and the software or systems that control them. It includes physical connectors, communication protocols, and drivers that allow software to communicate with hardware devices.
- Network Interface: This refers to the connection point between a computer or device and a network. It includes network cards, Ethernet ports, and wireless adapters that enable communication between devices on a network.
- User Interface (UI): This is the interface through which users interact with software applications or systems. It includes elements such as graphical user interfaces (GUIs), command-line interfaces (CLIs), and voice-based interfaces.
GUI(Graphical User Interface)
GUI stands for Graphical User Interface. It's a type of interface that allows users to interact with electronic devices through graphical icons and visual indicators, as opposed to text-based interfaces, typed command labels, or text navigation. GUIs utilize visual elements such as windows, buttons, menus, and icons to represent information and actions available to the user. GUIs are commonly used in software applications, operating systems, and embedded systems to provide an intuitive and user-friendly interface for performing tasks and accessing functionalities. Examples of GUI-based operating systems include Windows, macOS, and various Linux distributions.
Group Assignment
This week, our group worked on creating ways for computers to talk to our microcontroller board (think of it like the brain of a small electronic device) using different tools.
Website Control
One tool we used was making a website that we could open on our phones or computers. This website could control things connected to our microcontroller board.
MIT App Inventor
Another tool we explored was MIT App Inventor. It's like a simple online tool that helps you build apps for Android phones. We learned how to use it to design an app that could control our microcontroller board. It was pretty cool because we could see how the app would work in real-time on our phones while we were building it.
Individual Assignment
I thank Ms.Zina and my friend Ngawang Pemo whose documentation guided me through the process
My friend Ngawang found Ms.Zina's documentation very helpful and easy to comprehend, so I refered to both of their documentations and also decided to create a GUI with Tkinter as my framework to control my LED on my board from embedded programming week.
Tkinter
What is Tkinter?
Tkinter is a standard Python library for creating graphical user interfaces (GUIs). It provides a set of tools and widgets to create windows, buttons, labels, textboxes, menus, and more. Tkinter is based on the Tk GUI toolkit, which originated as the GUI extension for the Tcl scripting language.
Key Points about Tkinter:
- Cross-platform: Tkinter is included with most Python installations on various platforms like Windows, macOS, and Linux, making it a cross-platform solution for GUI development.
- Simple and Easy to Use: Tkinter's API is straightforward and relatively easy to learn, making it accessible for beginners to create basic GUI applications.
- Extensible: While Tkinter provides a set of basic widgets, it can be extended with additional third-party packages like ttk (Themed Tkinter), which offers modern-looking widgets, and other libraries for more advanced functionality.
- Integration with Python: Since Tkinter is part of the Python standard library, it integrates seamlessly with Python code, allowing developers to leverage Python's extensive libraries and tools for GUI development.
To begin,download python with your prefered settings.
To check whether python was successfully installed on your device, open "command prompt" on your device.
Use the command below and get the information on the version of python installed on your device.
python
Getting strted with Tkinter
Start with creating a python file in VS Code To do that, start by opening VS Code, and then click "ctrl+s" and an option of saving it will pop out. Set the name as whatever you want to call it, then choose python as your file type.
Since, this was my first time getting to know Tkinter I watched a beginner's course on python GUI development
Some fundamental codes I learned from the tutorial would be:
Importing Tkinter:
import tkinter as tk
Creating a main window:
root = tk.Tk()
Adding widgets:
label = tk.Label(root, text="Hello, Tkinter!")
button = tk.Button(root, text="Click me!")
Organizing widgets using geometry managers:
Pack manager:
label.pack()
button.pack()
Grid manager:
label.grid(row=0, column=0)
button.grid(row=1, column=0)
Configuring widget properties:
label.config(font=("Arial", 12), fg="blue")
button.config(bg="green", command=some_function)
Defining event handlers:
def some_function():
print("Button clicked!")
Running the main event loop:
root.mainloop()
To run a Tkinter code for a basic application, you need a python extention that can be downloaded in VS Code.
The code below ran successfully and the basic application showed up.
import tkinter as tk
root = tk.Tk()
root.mainloop()
Now, I would like to try designing my own GUI before continuing with the LED switch
Getting to know GUI
So, to get to know GUI better, I decided to make a GUI where it receives the DOB of a person and displays their age.
Run the Tkinter python code below
import tkinter as tk
from tkinter import ttk
from datetime import datetime
def calculate_age(dob):
today = datetime.today()
age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
return age
def action():
username = name_var.get()
user_dob_str = dob_var.get()
user_gender = gender_var.get()
# Calculate age based on DOB
try:
dob = datetime.strptime(user_dob_str, "%Y-%m-%d")
age = calculate_age(dob)
age_text = f"{age} years old"
except ValueError:
age_text = "Invalid DOB"
result_text = f"Name: {username}\nDOB: {user_dob_str}\nGender: {user_gender}\nAge: {age_text}"
result_label.config(text=result_text)
with open('file.txt', 'a') as f:
f.write(f'{username},{user_dob_str},{user_gender},{age_text}\n')
name_entrybox.delete(0, tk.END)
dob_entrybox.delete(0, tk.END)
name_label.configure(foreground='#991E1E')
submit_button.configure(foreground='Blue')
win = tk.Tk()
win.title('Age Calculator')
header_label = ttk.Label(win, text='Age Calculator', font=('Arial', 18, 'bold'))
header_label.pack()
name_label = ttk.Label(win, text='Enter your name')
name_label.pack()
name_var = tk.StringVar()
name_entrybox = tk.Entry(win, width=40, textvariable=name_var)
name_entrybox.pack()
dob_label = ttk.Label(win, text='Enter your DOB (YYYY-MM-DD)')
dob_label.pack()
dob_var = tk.StringVar()
dob_entrybox = tk.Entry(win, width=40, textvariable=dob_var)
dob_entrybox.pack()
gender_label = ttk.Label(win, text='Select your gender')
gender_label.pack()
gender_var = tk.StringVar()
gender_combobox = ttk.Combobox(win, width=38, textvariable=gender_var, state='readonly')
gender_combobox['values'] = ('Male', 'Female', 'Other')
gender_combobox.current(0)
gender_combobox.pack()
submit_button = tk.Button(win, text='Submit', command=action, width=20, bg='blue', fg='white')
submit_button.pack()
result_label = ttk.Label(win, text='', font=('Arial', 12))
result_label.pack()
win.mainloop()
Here is how, I got an idea of how GUI works
I got a little carried away, and I tried my friend's trial GUI code and combined it with mine
Here is her code(to make a GUI where it receives the DOB of a person and displays their zodiac sign)
import tkinter as tk
from tkinter import ttk
from datetime import datetime
def calculate_zodiac_sign(dob):
month, day = dob.month, dob.day
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
return "Aries"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
return "Taurus"
elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
return "Gemini"
elif (month == 6 and day >= 21) or (month == 7 and day <= 22):
return "Cancer"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return "Leo"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return "Virgo"
elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
return "Libra"
elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
return "Scorpio"
elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
return "Sagittarius"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
return "Capricorn"
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
return "Aquarius"
else:
return "Pisces"
def action():
username = name_var.get()
user_dob_str = dob_var.get()
user_gender = gender_var.get()
# Calculate zodiac sign based on DOB
try:
dob = datetime.strptime(user_dob_str, "%Y-%m-%d")
zodiac_sign = calculate_zodiac_sign(dob)
except ValueError:
zodiac_sign = "Invalid DOB"
result_text = f"Name: {username}\nDOB: {user_dob_str}\nGender: {user_gender}\nZodiac Sign: {zodiac_sign}"
result_label.config(text=result_text)
with open('file.txt', 'a') as f:
f.write(f'{username},{user_dob_str},{user_gender},{zodiac_sign}\n')
name_entrybox.delete(0, tk.END)
dob_entrybox.delete(0, tk.END)
name_label.configure(foreground='#991E1E')
submit_button.configure(foreground='Blue')
win = tk.Tk()
win.title('Zodiac Sign Calculator')
header_label = ttk.Label(win, text='Zodiac Sign Calculator', font=('Arial', 18, 'bold'))
header_label.pack()
name_label = ttk.Label(win, text='Enter your name')
name_label.pack()
name_var = tk.StringVar()
name_entrybox = tk.Entry(win, width=40, textvariable=name_var)
name_entrybox.pack()
dob_label = ttk.Label(win, text='Enter your DOB (YYYY-MM-DD)')
dob_label.pack()
dob_var = tk.StringVar()
dob_entrybox = tk.Entry(win, width=40, textvariable=dob_var)
dob_entrybox.pack()
gender_label = ttk.Label(win, text='Select your gender')
gender_label.pack()
gender_var = tk.StringVar()
gender_combobox = ttk.Combobox(win, width=38, textvariable=gender_var, state='readonly')
gender_combobox['values'] = ('Male', 'Female', 'Other')
gender_combobox.current(0)
gender_combobox.pack()
submit_button = tk.Button(win, text='Submit', command=action, width=20, bg='blue', fg='white')
submit_button.pack()
result_label = ttk.Label(win, text='', font=('Arial', 12))
result_label.pack()
win.mainloop()
Here is the combined code(to make a GUI where it recieves the DOB of a persona nd displays their age and zodiac sign)
import tkinter as tk
from tkinter import ttk
from datetime import datetime
def calculate_age(dob):
today = datetime.today()
age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
return age
def calculate_zodiac_sign(dob):
month, day = dob.month, dob.day
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
return "Aries"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
return "Taurus"
elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
return "Gemini"
elif (month == 6 and day >= 21) or (month == 7 and day <= 22):
return "Cancer"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return "Leo"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return "Virgo"
elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
return "Libra"
elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
return "Scorpio"
elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
return "Sagittarius"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
return "Capricorn"
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
return "Aquarius"
else:
return "Pisces"
def action():
username = name_var.get()
user_dob_str = dob_var.get()
user_gender = gender_var.get()
# Calculate age based on DOB
try:
dob = datetime.strptime(user_dob_str, "%Y-%m-%d")
age = calculate_age(dob)
age_text = f"{age} years old"
except ValueError:
age_text = "Invalid DOB"
# Calculate zodiac sign based on DOB
try:
zodiac_sign = calculate_zodiac_sign(dob)
except ValueError:
zodiac_sign = "Invalid DOB"
result_text = f"Name: {username}\nDOB: {user_dob_str}\nGender: {user_gender}\nAge: {age_text}\nZodiac Sign: {zodiac_sign}"
result_label.config(text=result_text)
with open('file.txt', 'a') as f:
f.write(f'{username},{user_dob_str},{user_gender},{age_text},{zodiac_sign}\n')
name_entrybox.delete(0, tk.END)
dob_entrybox.delete(0, tk.END)
name_label.configure(foreground='#991E1E')
win = tk.Tk()
win.title('Age and Zodiac Sign Calculator')
header_label = ttk.Label(win, text='Age and Zodiac Sign Calculator', font=('Arial', 18, 'bold'))
header_label.pack()
name_label = ttk.Label(win, text='Enter your name')
name_label.pack()
name_var = tk.StringVar()
name_entrybox = tk.Entry(win, width=40, textvariable=name_var)
name_entrybox.pack()
dob_label = ttk.Label(win, text='Enter your DOB (YYYY-MM-DD)')
dob_label.pack()
dob_var = tk.StringVar()
dob_entrybox = tk.Entry(win, width=40, textvariable=dob_var)
dob_entrybox.pack()
gender_label = ttk.Label(win, text='Select your gender')
gender_label.pack()
gender_var = tk.StringVar()
gender_combobox = ttk.Combobox(win, width=38, textvariable=gender_var, state='readonly')
gender_combobox['values'] = ('Male', 'Female', 'Other')
gender_combobox.current(0)
gender_combobox.pack()
submit_button = tk.Button(win, text='Submit', command=action, width=20, bg='blue', fg='white')
submit_button.pack()
result_label = ttk.Label(win, text='', font=('Arial', 12))
result_label.pack()
win.mainloop()
I fed my inner childness, hehe.
Moving to the LED switch
After being able to code for the age calculator, I tried making a interface to control my LED on my embedded programming week's board.
Firstly upload the following code on Arduino IDE using a programmer, I uploaded the below to my board using Arduino as ISP.
int led = 2;
char mydata = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
mydata = int(Serial.read());
if (mydata == '1')
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
if (mydata == '0')
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
change the line,
int led = 2;
accordingly to which pin your LED is connected to.
Then connected your board to the laptop with FTDI cable.
And run this code the code below like before in VS Code for the GUI
import serial
from tkinter import *
def led_on():
arduino_data.write(b'1')
def led_off():
arduino_data.write(b'0')
led_control_window = Tk()
led_control_window.title('LED Control')
btn_on = Button(led_control_window, text='Turn LED On', command=led_on)
btn_on.pack(pady=10)
btn_off = Button(led_control_window, text='Turn LED Off', command=led_off)
btn_off.pack(pady=10)
arduino_data = serial.Serial('COM18', 9600)
led_control_window.mainloop()
change the line,
arduino_data = serial.Serial('COM18', 9600)
accordingly to which COM Port FTDI is connected