LoginSignup
2
0

More than 3 years have passed since last update.

ストップウォッチです

Last updated at Posted at 2020-07-10
1 / 8

はじめに

カップラーメンが好きで、3分間測るために
ストップウォッチを作ろうと思いました。

stop watch.gif

これが完成形です。


作り方

2つのライブラリをインポートする

import tkinter as tk
import time

ウィンドウを作る

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) #ウィンドウを固定サイズに
    app = Application(master=win)
    app.mainloop()

if __name__ == "__main__":
    main()

タイトル:stop watch
サイズ:300*150
背景色:黒


ボタンを作る

        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)

ボタンの名前や大きさを指定する


完成形のプログラム

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) #ウィンドウを固定サイズに
    app = Application(master=win)
    app.mainloop()

if __name__ == "__main__":
    main()

このソースコードは
https://qiita.com/michimichix521/items/76234e7a991ab92e6fb3
これを参考させてもらいました。
ストップウォッチの色を自分の好きなものにしたり
ボタンの配置を自分が押しやすいように並び替えました。


最後に

このストップウォッチは秒単位でしか測れなくて分での表示ができません。
また1/100秒のくらいまで表示してみたいと思っていましたが、
いろいろ検索してもやり方が分かりませんでした。
Pythonをもっと勉強して次何かを作るときは、自分が思っているようにコーディングできるようになりたいと思いました。

参考文献


2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0