LoginSignup
25
30

More than 5 years have passed since last update.

messageboxとsimpledialogのまとめ

Posted at

tkinterで用意しているmesseagebox、simpledialogのまとめです。

import

まず、tkinter、messeagebox、simpledialogをインポートします。

messagebox.py
import tkinter as tk
from tkinter import messagebox
import tkinter.simpledialog as simpledialog

続いて、小さなウィンドウを表示させない設定をします。

messagebox.py
root = tk.Tk()
root.withdraw() #小さなウィンドウを表示させない

messagebox

tkinterのmessageboxでは、7つのmessageboxが用意されています。

  1. showinfo
  2. showwarning
  3. showerror
  4. askquestion
  5. askokcancel
  6. askyesno
  7. askretrycansel

showinfo

messagebox.py
# showinfo
res = messagebox.showinfo("title", "message")
print("showinfo", res)

showinfo.PNG

[OK]を押した時の戻り値はok

showinfo ok

showwarning

messagebox.py
# showwarning
res = messagebox.showwarning("title", "message")
print("showwarning", res)

showwarning.PNG

[OK]を押した時の戻り値はok

showwarning ok

showerror

messagebox.py
# showerror
res = messagebox.showerror("title", "message")
print("showerror", res)

showerror.PNG

[OK]を押した時の戻り値はok

showerror ok

askquestion

messagebox.py
# askquestion
res = messagebox.askquestion("title", "message")
print("askquestion", res)

askquestion.PNG

[はい]を押した時の戻り値はyes
[いいえ]を押した時の戻り値はno

askquestion yes
askquestion no

askcancel

messagebox.py
# askokcancel
res = messagebox.askokcancel("title", "message")
print("askokcancel", res)

askcancel.PNG

[OK]を押した時の戻り値はTrue
[キャンセル]を押した時の戻り値はFalse

askokcancel True
askokcancel False

askyesno

messagebox.py
#askyesno
res = messagebox.askyesno("title", "message")
print("askyesno", res)

askyesno.PNG

[はい]を押した時の戻り値はTrue
[いいえ]を押した時の戻り値はFalse

askyesno True
askyesno False

askretrycancel

messagebox.py
#askretrycancel
res = messagebox.askretrycancel("title", "message")
print("askretrycancel", res)

askretrycancel.PNG

[再試行]を押した時の戻り値はTrue
[キャンセル]を押した時の戻り値はFalse

askretrycancel True
askretrycancel False

simpledialog

ユーザに値を入力させるためのダイアログボックスです。

messagebox.py
#simpledialog
inputdata = simpledialog.askstring("Input Box", "値を入力してください",)
print("simpledialog",inputdata)

dialog.PNG

戻り値は入力した値
[キャンセル]を押した時の戻り値はNone

simpledialog ABC
simpledialog 123
simpledialog None
25
30
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
25
30