14. application programming¶
This week’s assignment write an application that interfaces a user with an input and/or output device that you made.
Python¶
I use the Python Programming Language to creat the application .
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics
print("Hello World")
print("This is my first program in Python")
print("I am using the function print")
print("Print outputs text and data into the console")
print()
print()
message = "this is a string"
print("print a string")
print(message)
after learning the python language i well write a code for my application
so to use python i learne the libery tkinter
First, we will import Tkinter package and create a window and set its title:
from tkinter import *
window = Tk()
window.title("Welcome to fab academy app")
lbl = Label(window, text="Hello")
lbl.grid(column=0, row=0)
window.mainloop()
work in application
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.tkButton1 = tk.Button(self)
self.tkButton1["text"] = "tempurature"
self.tkButton1["command"] = self.temeperature()
self.tkButton1.pack()
self.tkButton4 = tk.Button(self)
self.tkButton4["text"] = "humidity"
self.tkButton4.pack()
self.tkButton5 = tk.Button(self)
self.tkButton5["text"] = "wind"
self.tkButton5.pack()
self.tkLabel = tk.Label(self)
self.tkLabel["text"] = "This a label"
self.tkLabel.pack()
self.tkEntry = tk.Entry(self)
self.tkEntry.pack()
self.tkButton2 = tk.Button(self, text="data", fg="red", command=self.master.destroy)
self.tkButton2.pack()
root = tk.Tk()
app = Application(master=root)
app.master.title("wather station")
app.master.geometry("300x300")
app.mainloop()
```
![](../app.png)
i well use the serial of arduino and programme with python to connecte the input and output
connect the arduino to Ultrasonic Sensor HC-SR04
the code of arduino
byte trigPin = 7; byte echoPin = 6; long duration; int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); distance= duration*0.034/2;
if( distance < 250 && distance > 3) Serial.println(distance); else Serial.println(20);
delay(20);
}
import tkinter as tk import serial
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.mySerial = serial.Serial('COM10') # open serial port
self.create_widgets()
def create_widgets(self):
self.tkCanvas = tk.Canvas(self, bg="blue", height=100, width=300)
self.tkCanvas.pack()
self.tkFrame = tk.Frame(self)
self.tkFrame.pack()
self.tkLabel = tk.Label(self.tkFrame)
self.tkLabel.pack()
self.tkButton1 = tk.Button(self.tkFrame, text="Exit", command=self.quit)
self.tkButton1.pack()
def beji(self):
line = str(self.mySerial.readline())
line = line[2:]
line = line[:len(line) - 5]
self.tkLabel["text"] = line
self.tkCanvas.delete("all")
self.tkCanvas.create_rectangle(10, 10, int(line)+20, 90, fill="#476042")
def quit(self):
self.mySerial.close()
self.master.destroy()
root = tk.Tk() app = Application(master=root) app.master.title(“Arduino sonar”) app.master.geometry(“300x200”)
while True: app.beji() app.update_idletasks() app.update()
```