[PYTHON] It's a stopwatch

Introduction

I like cup ramen and want to measure for 3 minutes I wanted to make a stopwatch.

stop watch.gif

This is the completed form.


How to make

Import two libraries

import tkinter as tk
import time

Make a window

import tkinter as tk

class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        master.geometry("300x150")
        master.title("STOP WATCH")
        master.config(bg="black")

def main():
    win = tk.Tk()
    #win.resizable(width=False, height=False) #Fixed size window
    app = Application(master=win)
    app.mainloop()

if __name__ == "__main__":
    main()

Title: stop watch Size: 300 * 150 Background color: black


Make a button

        tk.Button(master,text="start",command=self.resetButtonClick,width=10).place(x=10, y=110)
        tk.Button(master,text="stop",command=self.startButtonClick,width=10).place(x=110, y=110)
        tk.Button(master,text="reset",command=self.stopButtonClick,width=10).place(x=210, y=110)

Specify the button name and size


Complete program

import tkinter as tk
import time

class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        master.geometry("300x150")
        master.title("STOP WATCH")
        master.config(bg="black")

        self.startTime=time.time()
        self.stopTime=0.00
        self.elapsedTime=0.00
        self.playTime=False

        self.canvas = tk.Canvas(master,width=290,height=80,bg="silver")
        self.canvas.place(x=3,y=10)

        tk.Button(master,text="start",command=self.resetButtonClick,width=10).place(x=10, y=110)
        tk.Button(master,text="stop",command=self.startButtonClick,width=10).place(x=110, y=110)
        tk.Button(master,text="reset",command=self.stopButtonClick,width=10).place(x=210, y=110)

        master.after(50,self.update)

    def startButtonClick(self):
        if self.playTime:
            self.stopTime=time.time()-self.startTime
            self.playTime=False

    def stopButtonClick(self):
        self.startTime=time.time()
        self.stopTime=0.00
        self.elapsedTime=0.00
        self.playTime=False

    def resetButtonClick(self):
        if not self.playTime:
            self.startTime=time.time()-self.elapsedTime
            self.playTime=True

    def update(self):
        self.canvas.delete("Time")
        if self.playTime:
            self.elapsedTime=time.time()-self.startTime
            self.canvas.create_text(280,40,text=round(self.elapsedTime,1),font=("Helvetica",40,"bold"),fill="black",tag="Time",anchor="e")
        else:
            self.canvas.create_text(280,40,text=round(self.stopTime,1),font=("Helvetica",40,"bold"),fill="black",tag="Time",anchor="e")

        self.master.after(50,self.update)

def main():
    win = tk.Tk()
    #win.resizable(width=False, height=False) #Fixed size window
    app = Application(master=win)
    app.mainloop()

if __name__ == "__main__":
    main()

This source code is https://qiita.com/michimichix521/items/76234e7a991ab92e6fb3 I was allowed to refer to this. You can change the color of the stopwatch to your liking I rearranged the button layout to make it easier for me to press.


Finally

This stopwatch can only measure in seconds and cannot display in minutes. I also wanted to display it up to about 1/100 second, I couldn't figure out how to do it after various searches. When I studied Python more and made something next, I wanted to be able to code as I wanted.

References

https://qiita.com/michimichix521/items/76234e7a991ab92e6fb3


Recommended Posts

It's a stopwatch
Stopwatch
It's a Mac. What is the Linux command Linux?