And now for something completely different!¶
It is my believe that the best way to learn is to learn as you go.
So, we are going to make a small clock program. This will tell us the time in an analog fashion. So we are going to need a few things to get started, I find by drawing out and writing down your goals it will help better guide the development.
GOALS:
- Read the time from the system.
- Display the time to a window.
- Loop the display finction until we press 'esc' or close the window.
So this is the basics we will start with. Later we will upgrade our script to decorate the hands of the clock.
First we need Python¶
So do yourself a favor, and go get Jupyter-Lab, it is what I am using to make these tutorials and I am really enjoying it. It will also make it easier to follow along, and will allow you to just copy and paste the code into a code cell and run it.
So Anaconda Individual 64-bit click the link, download and install. There are install iunstructions available on the site so we will brush past that.
Starting Jupyter-lab¶
Just startup the Anacond Prompt, and type jupyter-lab
, a browser tab/window will pop up and you will be greeted with the insterface.
So our first task is we need to create a program, this is a task that is usually helped out by a framework, for me I will use pyQT5, documentation can be found online.
So first we create a basic program¶
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
So it is a bit to take in, and at this stage we could execute the code but not much will happen. So we import the libraries we will be using. Libraries are like extra tools in your toolbox. They are usually created to help the programmer create a bit faster. However, they can also allow non python stuff to work in python. For example, the Raspbery Pi contains GIOP pins that allow you to connect things like an LED to the board, by importing the GIOP library, one that program the pins to turn the LED on/off as desired.
If you look up each in the reference for these libraries you should be able to figure our what they do. I will explain as we go. In short we are importing libraries that will help us create a window, propigate that window with a canvas that we can display something to. I will admit I may not cover all the details for each method in each library, that would get boring really quick and frankly I just do not have all that knowledge jammed into my tiny brain box. This is why I encourage anyone to get familiar with reading reference documentation. Feeling comfortable with navigating them and finding the information you need is the most valuable thing I could teach anybody when it comes to programming.
Furthermore, I may also choose to leave some things out, either due to environment differences, (I did not get that error, so I cannot fix it) or because I see the value in the act of discovery. Needless to say, Each code block will be executable, you can run them and see the result. I will be building each project in a way that mimics how I slowly chip away and climb the mountain that is writing a program. However, I am just starting with making these tutorials and I encourage anyone to comment and let me know where I can improve them.
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# create a clock class
class Clock(QMainWindow):
# create a constructor
def __init__(self):
super().__init__()
# Driver code
if __name__ == '__main__':
app = QApplication(sys.argv)
# creating a clock object
win = Clock()
# show
win.show()
exit(app.exec_())
Now we have something to run, it isn't much but you should see a window like in the following picture. But what have we done? So this is a basic boilerplate application in python using PyQt5. We are creating a class called Clock()
we pass a method which comes from the QtWidgets library called QMainWindow
. But its not enough to just call the window function we have to use it too. We do that with the creating of the constructor. This is where the sys library comes in. We define a self initiating function. So now we have called the method, allowed it to create itself, now we use it with the driver code. This creates a window app named main that calls our clock function and displays that.
Comments
Post a Comment