PythonとTkinterでカウントダウンタイマーを作成する
Tkinterは、GUIベースのデスクトップアプリケーションを作成するための標準のPythonライブラリです。アプリケーションの機能を実装するために使用できるさまざまな機能、モジュール、およびメソッドを提供します。
この例では、TkinterやtimemoduleなどのPython標準ライブラリを使用してカウントダウンTimeを作成します。このアプリケーションの基本的な機能は、特定の期間タイマーを実行することです。次のコンポーネントが含まれます
-
HH / MM/SSごとにタイマーを設定するエントリウィジェット。
-
関数countdowntimer()を実行するためのボタン 。
-
関数countdowntimer() 入力文字列をHH、MM、およびSSを基準にした整数値に変換します。
-
update()の使用 メソッドでは、指定された関数とウィジェットに関してウィンドウを更新します。
例
# Import the required library
from tkinter import *
import time
# Create an instance of tkinter frame
win = Tk()
# Set the size of the window
win.geometry('700x350')
# Make the window fixed to its size
win.resizable(False, False)
# Configure the background
win.config(bg='skyblue4')
# Create Entry Widgets for HH MM SS
sec = StringVar()
Entry(win, textvariable=sec, width=2,
font='Helvetica 14').place(x=380, y=120)
sec.set('00')
mins = StringVar()
Entry(win, textvariable=mins, width=2, font='Helvetica 14').place(x=346, y=120)
mins.set('00')
hrs = StringVar()
Entry(win, textvariable=hrs, width=2, font='Helvetica 14').place(x=310, y=120)
hrs.set('00')
# Define the function for the timer
def countdowntimer():
times = int(hrs.get()) * 3600 + int(mins.get()) * 60 + int(sec.get())
while times > -1:
minute, second = (times // 60, times % 60)
hour = 0
if minute > 60:
hour, minute = (minute // 60, minute % 60)
sec.set(second)
mins.set(minute)
hrs.set(hour)
# Update the time
win.update()
time.sleep(1)
if (times == 0):
sec.set('00')
mins.set('00')
hrs.set('00')
times -= 1
# Create a Label widget
Label(win, font=('Helvetica bold', 22), text='Set the Timer', bg='skyblue4', fg="white").place(x=260, y=70)
# Button widget to set the timer
Button(win, text='START', bd='2', bg='IndianRed1', font=('Helvetica bold', 10), command=countdowntimer).place(x=335, y=180)
win.mainloop() 出力
ウィンドウにカウントダウンタイマーが表示されます。
入力ボックスの値を変更してタイマーを設定し、[開始]ボタンをクリックすると、指定された期間のタイマーがすばやく開始されます。
-
一定のサイズでTkinterウィンドウを設定するにはどうすればよいですか?
ウィジェットのサイズに応じて、tkinterフレームのサイズが自動的に変更される場合があります。フレームのサイズを一定にするには、ウィジェットを停止してフレームのサイズを変更する必要があります。したがって、3つの方法があります ブール値pack_propagate(True / False) メソッドは、ウィジェットからのフレームのサイズ変更を防ぎます。 サイズ変更可能(x、y) メソッドは、ウィンドウのサイズが変更されないようにします。 パック(塗りつぶし、展開) ウィンドウをジオメトリで定義されたサイズにサイズ変更する値。 基本的に、tkinterフレーム内のすべて
-
Pythonプログラムによるデータ分析と視覚化
このチュートリアルでは、パンダなどのモジュールを使用したデータ分析と視覚化について学習します。 およびmatplotlib Python 。 Pythonは、データ分析に最適です。モジュールをインストールするパンダ およびmatplotlib 次のコマンドを使用します。 pip install pandas pip install matplotlib インストールプロセスが完了すると、成功メッセージが表示されます。まず、パンダについて学びます その後、 matplotlibが表示されます 。 パンダ Pandasは、データ分析ツールを提供するPythonのオープンソース