? ? ? ? 前面寫過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'
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
