Как сделать часы на python
Перейти к содержимому

Как сделать часы на python

  • автор:

how to make a digital clock in python

In this article, I’m going to share with you how to build a digital clock using Python programming in just a few lines of code using the Tkinter and Time module.

Requirements

If you’re on window OS you don’t really need to install anything since our simple digital clock app will be using only built-in modules time and Tkinter

But if you’re running Linux OS most of the time pre installed python interpreter doesn’t come with the Tkinter installed, therefore you might need to install it manually just as shown below;

Installation

Exit fullscreen mode

Now that everything is installed, lets’s build our digital clock,

Let’s get started .

The idea is really simple, firstly we will create an empty window using the Tkinter, then we gonna configure and place a Label widget within our empty window, and then finally we will be updating the value of the label to be the current time after every 0.08s.

getting current time .

Time module provides a variety of ways of getting time, In this article, we are going to use* strftime() to parse the current time into the *Hour: Minutes: Seconds format.

Example of usage

Exit fullscreen mode

Now lets add graphical interface

Previous experience with the Tkinter library would be an advantage for you, but even if this is your first time still you can make it, the syntax is intuitive and straight forward.

Making empty window with tkinter

In making an empty clock window, we gonna use geometry () to specify the dimension of the displayed window, and at the end put mainloop() to prevent the displayable window from exiting immediately.

Exit fullscreen mode

When you run the above code, it will produce an interface like this;

Alt Text

Adding some logic to our code ..

Now that we have our empty window, let’s now add a label to place our time information together with a function to update the value on the label every 80 milliseconds.

Exit fullscreen mode

why 80 milliseconds?

According to research human brain can only process 12 separate images per second, anything more than that it will be perceived as a single picture, this is what causes the illusion of motion.

Below is the full code of our digital clock, with you, can try changing the code parameter the way you would like and then press run again.

Exit fullscreen mode

Once you run the above lines of code, you should be seeing a widget with clock details rendered on your machine similar to that screenshot below;

Alt Text

Based on your interest I recommend you to also check these;

  • How to make a music player in python
  • How to track phone number in Python
  • How to make a python GUI calculator in Python
  • How to make a guessing game in Python
  • How to program Arduino board with Python
  • How to build a website blocker in Python

Kalebu / Digital-clock-in-Python

A digital clock application made with python and tkinter

Digital-clock-in-Python

Intro

This is a code for a simple digital clock made in python with help of the time module and Tkinter library

How to run

Firstly download or clone this repo and then move into the project folder as shown below;

Exit fullscreen mode

Output

Once you run the code, it will render the output similar to what shown below;

digital_clock

This code is the continuation of a series of Python tutorial published on my blog, and the full article with code for can be found on Make a Digital Clock

Пишем GUI часы на Python tkinter

Пишем GUI часы на Python tkinter

Статьи

Введение

В ходе статьи напишем GUI часы на языке программирования Python с использованием модуля tkinter.

Написание кода GUI часов

Для начала импортируем модуль time и tkinter:

Далее создадим окно, добавим заголовок “Часы” и запретим возможность изменять размеры окна:

Создадим виджет Label(), расположим его на окне root, шрифт укажем Arial 70 размера и отобразим методом pack():

Теперь создадим функцию, которую назовём tick(). Внутри неё будет устанавливаться нынешнее время в Label(), а благодаря методу after() раз в секунду функцию будет вызываться рекурсивно:

Итоговый результат

Заключение

В ходе статьи мы с Вами написали код GUI часов на языке программирования Python. Надеюсь Вам понравилась статья, желаю удачи и успехов! ��

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *