Skip to main content

5 Unique and Useful Python modules.

Motive           There are different types of modules in python which not everyone knows so we thought of introducing them to you guys. Let's start! Note: This is not a tutorial of how to use these but is to show that these modules also exist and if you want to know more you can visit there own page and if you want us to explain leave a comment below. Links to there page are also given below. Hope you found something useful. 1.Wikipedia          Yes, there is a module called Wikipedia in python, you can easily install Wikipedia by pip install wikipedia . We cannot rely on this module for advanced use. It was designed for ease of use and simplicity.  Learn More Here .  Here is a simple example: import wikipedia wikiPython = wikipedia.summary("Python", sentences=2) print(wikiPython)           One big disadvantage is that it works only if the interne...

Digital Clock | Python | Pygame

 Motive 

        Just to keep this the way I make other projects on my website. Just ignore this and jump right into the project. Oh and here's a preview of what we are going to make.




The Project

        We will be using pygame to create our Digital Clock, I believe all of you have installed pygame, if not just open the command prompt and type pip install pygame and hit enter. Learn more about pygame here https://www.pygame.org/docs/. Feel free to skip to the end to get the full code.


         Okay back to the project, first, let's make a basic pygame window. So import pygame, initialize pygame, set an icon, make a screen like a rectangle then set its caption or whatever you call it, make a while loop to make it run till the exit button is pressed, and update the pygame display. Looks like a lot but these are necessary to even make a basic pygame project. Everyone who has at least worked with pygame once would know how it works.




        If you directly copy paste the code that I gave you might face an error in loading the icon. So you can go to https://icons8.com/ and find an appropriate icon for your Clock. If you want the same icon I used you can download it from here https://icons8.com/icons/set/digital-clock or if you don't want any icon you can simply comment it out or delete those lines. If you run the above code you will get a window like this,


      Now its time to get the time, so import datetime from datetime, it's preinstalled so no need for pip install.

      So again, import datetime then make a variable now and set it to datetime.now() then make a new variable called time and set it to now.strftime('%H:%M:%S'), here H stands for an hour, M for minute and S for second. You can change the format according to your wish. Now print(time).



        But the above code gives time in 24 hour format, I don't like it so I want to change it to 12 hour format. You can leave it like that if you want 24 hour format. For change it to 12 hour format we nee to split the time variable as minute and hour since these return number in string format we need to change it back to integer. Like this,



        Now we check if the variable hour is greater that 12 we subtract 12 from  hour, then create a new variable called time and store both these values as a string. Now the whole code should look like this,



        Now we need to display the time to the pygame window, for that we need to combine both codes, keeping in mind that we need to change the text every second, so keep those code in the while loop that we created. After all these, we need to make it display on the screen. For that we need a font, so create a variable called font and set it as pygame.font.SysFont('Comic Sans MS',130), here Comic Sans MS is the font and 130 is the size, feel free to change the font of your choice, just open Word and find a good font and replace Comic Sans with the new one you find I personally like comic san. Then make two variables white and black and set its value as (255,255,255) and (0,0,0) respectively it's the value for those colours then screen.fill(black) in the while loop. then render the text with the font that we made in the beginning and show it on the pygame window that's it, a simple Digital Clock. Here's the code:




That code will give you a clock like this,




        But that doesn't look cool, NO DATE, NO DAY, NO MONTH, NO AM/PM, nothing just the time that doesn't even look like a digital clock without the digital font and other necessary stuff. So back to that. Relax it was not a clickbait.


        First, let's begin by installing the digital font as it's not preinstalled in the system so go to https://www.dafont.com/ds-digital.font and install the zip file after that just extract it and select the 4 files that end with .TTF and right click then Install for all users, That's important as just install won't work. So we are done with all the installs. To ensure the font is installed, open Word and then search for DS-Digital if you find it you have successfully installed that font. (Other Resource for fonts- https://www.websiteplanet.com/blog/best-free-fonts/)

        We need that font in two different sizes so create two variable bigFont and smallFont and set it's size to 130 and 30 respectively. Then create a new variable called green for storing the value for the colour green, it's (0,255,0). Now we need to get the month it is in numbers as string from 1 to 12. So, get the value change it to an integer then make a list consisting of all the months in order as strings. Do the same for the day keeping in mind that 0 means Monday and rest follows. Like that, we will also find the year and store it in a variable. As we are creating the clock in the 12-hours format we will add AM/PM to it, for that make a variable called am and set it to 'AM' and if hour > 12 then we change AM to PM. Then place these variables in the window as your wish. With only the time in bigFont and the rest in smallFont.

This will result in a clock like this:



The Full Code


       If you have any doubts or errors in the program feel free to comment down below and I will respond as early as possible. And if I have made any mistake please correct me in the comment box below. Don't forget to follow us for such python related content.


~ Johan Jomy

Comments

Post a Comment

Popular posts from this blog

Chrome Dino Game Automation with Python.

Motive           I saw a video on YouTube that made a python program to automate the chrome dino game at the end he shows a clip of the program playing the game. But there was a problem with the code I do not work when it's dark so I thought of turning the dark mode in the laptop so that it stays dark all the time but that did not work after reaching a score of 700 it becomes day again so I have to think of an alternative see how I did it. The Project  First Import the necessary modules in your IDE import pyautogui from PIL import Image, ImageGrab import time PyAutoGUI   lets Python control the mouse and keyboard, and other GUI automation tasks.                    First, open your terminal and run pip  ...

Never Lose a Game of Chess | Python Chess Hack

Motive            I lost every game that I played chess with my friends. So why not cheat, even though  it was not the best option, I had to somehow prove that I was not dumb and can even win against them. Since they play online with me, I had a better chance to cheat so I thought of creating an ai to play against them but it was not that easy so I started surfing through the internet to find a better way. Then I found stockfish it was a chess engine that generates the best move for us. So I used it as my AI. Again, luck was with me I was able to find a module called chess in python that helped me a lot and now my work was really very easy. At last, I created the program and won the games against them but they had figured out that I was cheating and my friends started mocking at me. Then I understood that I was wrong and I shouldn't have cheated and started working hard for it and now I literally can win matches with ...