教程 > tkinter 教程 > 阅读:116

tkinter 教程 消息框——迹忆客-ag捕鱼王app官网

tkinter 消息框是在屏幕上弹出,给你额外信息或要求用户回答这样的问题 are you sure to quit? yes or no?


tkinter 消息框

#!/usr/bin/python3
import tkinter as tk
from tkinter import messagebox
 
messagebox.showinfo("basic example", "a basic tk messagebox")

tkinter 消息框_基本示例

from tkinter import messagebox

我们需要从 tkinter 导入 messagebox

messagebox.showinfo("basic example", "a basic tk messagebox")

showinfo 是 messagebox 中的显示函数之一。它在消息框中显示信息,其中 basic example 是标题,a basic tk messagebox 是所显示的信息。

tkinter messagebox 中的显示函数是

显示函数 描述
showinfo 普通信息
showwarning 警告信息
showerror 错误信息
askquestion 向用户提问
askokcancel 答案是 ok 和 cancel
askyesno 答案是 yes 和 no
askretrycancel 答案是 retry 和 cancel

tkinter 消息框示例

import tkinter as tk
from tkinter import messagebox
 
messagebox.showwarning("warning example", "warning messagebox")
messagebox.showerror("error example", "error messagebox")
messagebox.askquestion("ask question example", "quit?")
messagebox.askyesno("ask yes/no example", "quit?")
messagebox.askokcancel("ask ok cancel example", "quit?")
messagebox.askretrycancel("ask retry cancel example", "quit?")

tkinter 消息框_警告示例

tkinter 消息 box_error 示例

tkinter 消息框_askquestion

tkinter 消息框_askyesno

tkinter 消息框_askretrycancel

tkinter 消息框_askokcancel


gui 中的 tkinter 消息框示例

上面的消息框示例给我们展示了 tkinter 消息框的第一印象。但是通常消息框是在用户单击按钮后才会弹出。

我们将介绍如何将命令同消息框中的不同选项来绑定。

import tkinter as tk
from tkinter import messagebox
root= tk.tk()
root.geometry('300x200')
def exitapp():
    msgbox = tk.messagebox.askquestion ('exit app','really quit?',icon = 'error')
    if msgbox == 'yes':
       root.destroy()
    else:
        tk.messagebox.showinfo('welcome back','welcome back to the app')
        
buttoneg = tk.button (root, text='exit app',command=exitapp)
buttoneg.pack()
  
root.mainloop()

我们将构造消息框的函数 exitapp() 绑定到按钮 buttoneg。

if msgbox == 'yes':

askquestion 消息框中,单击的选项的返回值是 yesno

后续的操作可能是关闭应用程序,显示另一个消息框,或者其他已定义的行为。

tkinter 消息框_绑定到一个按钮

查看笔记

扫码一下
查看教程更方便
网站地图