Pythonのtkinterを使ってみる

Pocket

Yahoo!知恵袋で「Pythonでうるう年を判定するGUI」という質問があったので作ってみました。
基本的な部分はIntroduction · tkinterを使いました。ありがとうございました。

import tkinter as tk
import tkinter.messagebox as tkm

def main():
  root = tk.Tk()
  root.title("うるう年判定")
  root.geometry("400x100")
  my_label = tk.Label(text="西暦年を入力")
  my_label.pack()
  my_entry = tk.Entry(width=50)
  my_entry.pack()
  my_button = tk.Button(text="うるう年判定", width=50, command=lambda: show_message(my_entry.get()))
  my_button.pack()
  root.mainloop()

def show_message(text):
  if text.isdigit():
    year = int(text)
    if is_leap(year):
      result = "うるう年である"
    else:
      result = "うるう年でない"
    tkm.showinfo("info", result)

def is_leap(year):
  if year % 400 == 0:
    return True
  elif year % 100 == 0:
    return False
  elif year % 4 == 0:
    return True
  else:
    return False

if __name__ == "__main__":
  main()

[ 2021年7月10日 | カテゴリー: Python, デジタル ]

« | »

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

送信してください。


タグ

カテゴリー

最近の投稿

最近のコメント

固定ページ

アーカイブ

stabucky

写真

メタ情報