LoginSignup
18
22

More than 3 years have passed since last update.

【Python】Tkinterによる60行で作るGUIアプリ「ストップウォッチ」

Last updated at Posted at 2019-06-30

はじめに

こんにちは。:blush:
今回は「ストップウォッチ」を作っていきたいと思います。
完成するとこんな感じに動きます!

ストップウォッチ.gif

環境

  • Windows 10 home
  • Python 3.7.1

「ストップウォッチ」の制作

インポート

使うライブラリは以下の2つです。

import tkinter as tk
import time

ウィンドウの作成

import tkinter as tk
import time

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

        master.geometry("300x150")
        master.title("ストップウォッチ")
        master.config(bg="black")

def main():
    win = tk.Tk()
    app = Application(master = win)
    app.mainloop()

if __name__ == "__main__":
    main()

このプログラムでウィンドウを作っていきます。
サイズは300×150です。タイトル名はストップウォッチです。背景色を黒色にしています。
詳しくは【Python】Tkinterを使った雛形(クラス化手法)の記事をご参照ください。

完成したプログラム

ストップウォッチ.py
import tkinter as tk
import time

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

        master.geometry("300x150")
        master.title("ストップウォッチ")
        master.config(bg="black")

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

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

        tk.Button(master,text="リセット",command=self.resetButtonClick,width=10).place(x=10, y=110)
        tk.Button(master,text="スタート",command=self.startButtonClick,width=10).place(x=110, y=110)
        tk.Button(master,text="ストップ",command=self.stopButtonClick,width=10).place(x=210, y=110)

        master.after(50,self.update)

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

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

    def resetButtonClick(self):
        self.startTime=time.time()
        self.stopTime=0.0
        self.elapsedTime=0.0
        self.playTime=False

    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()

こちらが完成したプログラムになります。
作りながらの感想ですが、スタートする時間(このプログラムだとstartTime)が重要だと思いました。もしアルゴリズムがよく分からない場合は、startTimeの値は今何なのか?、その変化をよく見ていくと理解できるんじゃないかと思います!

おわりに

以上で「ストップウォッチ」は完成になります。
ここまで読んでいただき、ありがとうございました。

18
22
0

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
18
22