I tried to make a stopwatch using tkinter in python

I made a stopwatch with tkinter

Constitution

├── stop_watch.bat
├── code
│   └── main.py
└── sound
└── Appropriate music file.mp3

Body

main.py


import os
import time
import pickle

import tkinter as tk
import tkinter.font as tk_font
import threading
from pygame import mixer


class StopWatch:
    def __init__(self):
        self.prev_path = '../prev.pickle'
        self.sound_path = '../sound/Appropriate music file.mp3'

        self.time_value = self.load_pickle(self.prev_path)
        if self.time_value is None:
            self.time_value = 0
        self.starting_time_value = self.time_value

        w0 = 40
        y0 = 60
        y02 = y0 -20

        w1 = 80
        h1 = 40
        wspace = 10

        w2 = w1 * 3 + wspace * 2
        h2 = 50

        x1 = 40
        x2 = x1 + w1 + wspace
        x3 = x2 + w1 + wspace
        x0 = x3 + 60

        y1 = 10
        y2 = 95
        y3 = 140

        size1 = 56
        bg1 = '#ffffee'
        bg2 = '#ffffaa'
        bg0 = '#ffccff'
        bg02 = '#ffaaff'

        self.root = tk.Tk()
        self.enable_count_down = False

        self.root.title("Python GUI")
        self.root.geometry("340x215")

        self.time_text = tk.StringVar()
        self.time_label = tk.Label(self.root, textvariable=self.time_text, font=tk_font.Font(size=size1))
        self.time_label.place(x=x1, y=y1)

        button_up_1_hour = tk.Button(text="Time", command=self.plus_1_hour, font=tk.font.Font(size=14), bg=bg1)
        button_up_1_hour.place(x=x1, y=y2, width=w1, height=h1)

        button_up_1_min = tk.Button(text="Minutes", command=self.plus_1_min, font=tk.font.Font(size=14), bg=bg1)
        button_up_1_min.place(x=x2, y=y2, width=w1, height=h1)

        button_up_1_sec = tk.Button(text="Seconds", command=self.plus_1_sec, font=tk.font.Font(size=14), bg=bg1)
        button_up_1_sec.place(x=x3, y=y2, width=w1, height=h1)

        button_start = tk.Button(text="START/STOP", command=self.start_stop, font=tk.font.Font(size=14), bg=bg2)
        button_start.place(x=x1, y=y3, width=w2, height=h2)

        button_reset = tk.Button(text="RESET", command=self.reset, font=tk.font.Font(size=8), bg=bg0)
        button_reset.place(x=x0, y=y0, width=w0)

        button_again = tk.Button(text="AGAIN", command=self.again, font=tk.font.Font(size=8), bg=bg02)
        button_again.place(x=x0, y=y02, width=w0)

        thread = threading.Thread(target=self.count_down)
        thread.start()

        self.update()
        self.root.mainloop()

    def plus_1_hour(self):
        self.time_value += 60 * 60
        self.update()

    def plus_1_min(self):
        self.time_value += 60
        self.update()

    def plus_1_sec(self):
        self.time_value += 1
        self.update()

    def reset(self):
        self.time_value = 0
        self.enable_count_down = False
        self.update()

    def again(self):
        self.time_value = self.starting_time_value
        self.enable_count_down = False
        self.update()

    def sound(self):
        mixer.init()
        mixer.music.load(self.sound_path)
        mixer.music.play(1)

    def count_down(self):
        while True:
            if self.time_value > 0 and self.enable_count_down:
                self.time_value -= 1
                self.update()
            if self.time_value <= 0 and self.enable_count_down:
                self.time_value = 0
                self.enable_count_down = False
                self.update()
                self.sound()
            time.sleep(1)

    def start_stop(self):
        self.starting_time_value = self.time_value
        self.enable_count_down = not self.enable_count_down

    @staticmethod
    def sec_to_hms(sec):
        minutes, sec = divmod(sec, 60)
        hour, minutes = divmod(minutes, 60)
        return '{}:{:0>2}:{:0>2}'.format(hour, minutes, sec)

    def update(self):
        hms = self.sec_to_hms(self.time_value)
        self.time_text.set(hms)
        self.save_pickle(self.time_value, self.prev_path)

    @staticmethod
    def save_pickle(model, model_path, protocol=4):
        with open(model_path, mode='wb') as f:
            if protocol is None:
                pickle.dump(model, f)
            else:
                pickle.dump(model, f, protocol=protocol)

    @staticmethod
    def load_pickle(path):
        if os.path.exists(path):
            try:
                with open(path, mode='rb') as f:
                    obj = pickle.load(f)
                return obj
            except EOFError as e:
                print('load_pickle():', e)
                return None
        else:
            print('NOT FOUND load_pickle():', path)
            return None


if __name__ == '__main__':
    StopWatch()

Batch file that calls stop_watch.py

It is convenient if you create a batch file or shell script.

stop_watch.bat


start C:/Users/username/Anaconda3/envs/conda environment name/pythonw.exe ./code/main.py

Recommended Posts

I tried to make a stopwatch using tkinter in python
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to make a todo application using bottle with python
I tried to make a ○ ✕ game using TensorFlow
I tried to implement a pseudo pachislot in Python
I tried to make a simple mail sending application with tkinter of Python
I tried to implement a one-dimensional cellular automaton in Python
I tried "How to get a method decorated in Python"
I tried to make GUI tic-tac-toe with Python and Tkinter
I want to make input () a nice complement in python
I tried to make a simple text editor using PyQt
I tried to implement PLSA in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to make a Web API
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[Python] I tried to implement stable sorting, so make a note
[3rd] I tried to make a certain authenticator-like tool with python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to automate "one heart even if separated" using a genetic algorithm in Python
I want to create a window in Python
I tried playing a typing game in Python
I want to make a game with Python
[Memo] I tried a pivot table in Python
I tried to implement TOPIC MODEL in Python
I tried reading a CSV file using Python
I tried adding a Python3 module in C
I tried to implement selection sort in python
I tried to make a calculator with Tkinter so I will write it
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to develop a Formatter that outputs Python logs in JSON
A note I looked up to make a command line tool in Python
I tried to create a sample to access Salesforce using Python and Bottle
I tried to implement a card game of playing cards in Python
I tried to make PyTorch model API in Azure environment using TorchServe
I want to make a web application using React and Python flask
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
Make a bookmarklet in Python
[Python] I tried using OpenPose
I tried to make a "fucking big literary converter"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
Basics of I / O screen using tkinter in python3
I tried using TradeWave (BitCoin system trading in Python)
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
Continuation ・ I tried to make Slackbot after studying Python3
I want to randomly sample a file in Python
I tried to implement Dragon Quest poker in Python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python