前面写过python弹框的ctypes.windll.user32方法:https://blog.csdn.net/Gordennizaicunzai/article/details/78966694
该方法调用windows系统的user32组件,不是纯python方法,且wser32的MessageBoxA使用不当会出现乱码,不是很好用。这里介绍pythonic的弹框——tkinter.messagebox。
NAME
tkinter.messagebox
DESCRIPTION
# tk common message boxes
#
# this module provides an interface to the native message boxes
# available in Tk 4.2 and newer.
#
# written by Fredrik Lundh, May 1997
#
CLASSES
tkinter.commondialog.Dialog(builtins.object)
Message
class Message(tkinter.commondialog.Dialog)
| Message(master=None, **options)
|
| A message box
|
| Method resolution order:
| Message
| tkinter.commondialog.Dialog
| builtins.object
|
| Data and other attributes defined here:
|
| command = 'tk_messageBox'
|
| ----------------------------------------------------------------------
| Methods inherited from tkinter.commondialog.Dialog:
|
| __init__(self, master=None, **options)
| Initialize self. See help(type(self)) for accurate signature.
|
| show(self, **options)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from tkinter.commondialog.Dialog:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
FUNCTIONS
showinfo(title=None, message=None, **options)
Show an info message
注意,tkinter.messagebox默认主窗口是显示状态,我们弹框时它是多于的,需要隐藏。
>>> import tkinter as tk
>>> from tkinter import messagebox as messagebox
>>>
>>> messagebox.showinfo('弹框', '注意,主窗口未退出')
'ok'
>>>
隐藏主窗口,函数要放在弹框前,否则无效:
showerror(title=None, message=None, **options)
Show an error message
>>> tk.Tk().withdraw() # 隐藏主窗口
''
>>> messagebox.showerror('弹框', 'say something')
'ok'
>>>
showwarning(title=None, message=None, **options)
Show a warning message
>>> tk.Tk().deiconify() # 显示主窗口
''
>>> messagebox.showwarning('弹框', 'say something')
'ok'
>>>
askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
选择确定/取消,返回值分别对应true/false,下同:
>>>
>>> tk.Tk().withdraw() # 隐藏主窗口
''
>>> msg = messagebox.askokcancel('对话框', '请选择')
>>> msg
False
>>>
askquestion(title=None, message=None, **options)
Ask a question
>>>
>>> msg = messagebox.askquestion('对话框', '请选择')
>>>
askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
msg = messagebox.askretrycancel('对话框', '请选择')
askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
msg = messagebox.askyesno('对话框', '请选择')
askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
>>> msg = messagebox.askyesnocancel('对话框', '请选择')
>>> msg
True
>>> msg = messagebox.askyesnocancel('对话框', '请选择')
>>> msg
False
>>> msg = messagebox.askyesnocancel('对话框', '请选择')
>>> msg
>>>
DATA
ABORT = 'abort'
ABORTRETRYIGNORE = 'abortretryignore'
CANCEL = 'cancel'
ERROR = 'error'
IGNORE = 'ignore'
INFO = 'info'
NO = 'no'
OK = 'ok'
OKCANCEL = 'okcancel'
QUESTION = 'question'
RETRY = 'retry'
RETRYCANCEL = 'retrycancel'
WARNING = 'warning'
YES = 'yes'
YESNO = 'yesno'
YESNOCANCEL = 'yesnocancel'