教程 > tkinter 教程 > 阅读:158

tkinter 对话框——迹忆客-ag捕鱼王app官网

我们将介绍弹出对话框,看看如何为不同的目的创建不同类型的对话框。

在 python 中,图形用户界面应用程序开发弹出对话框是小尺寸的窗口,可以用来显示消息框、错误消息框、警告消息、问题消息框等。

我们可以使用 tkinter 模块中的 messagebox 类生成弹出对话框。


在 tkinter 中使用 messagebox 创建不同的对话框

首先,我们将导入下面代码中所述的这两个。

from tkinter import *
from tkinter import messagebox

每当你想在 tkinter 中做任何事情时,你都必须设置一个窗口以开始使用 gui 窗口。下面的代码行将我们的窗口置于屏幕中央,位于所有内容的顶层。

gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())

下面的代码行指定一旦我们打开它就不会隐藏在另一个窗口后面。它将确保它与前面一样。

我们撤回我们的窗口,这将使我们的窗口不可见。如果不使用此代码,你将能够在弹出窗口后面看到另一个窗口。

gui.withdraw()

下一行使用接受两个字符串参数的 showinfo() 方法。第一个是标题,第二个是你要在此对话框中显示的消息。

messagebox.showinfo('information','you have created successfully the showinfo box')

最后,我们确保在最后的代码行中完成后退出我们的窗口,因此它不再显示在屏幕上。这是 showinfo 对话框的完整示例代码。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
messagebox.showinfo('information','you have created successfully the showinfo box')
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

showinfo()对话框

如果你希望显示错误对话框,你将输入 showerror() 消息框。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
# displays an error dialog box
messagebox.showerror('error','you have generated an error successfully')
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

showerror()对话框

另一个是一个警告对话框,就像你带着警告标志去的地方。使用 showwarning() 消息框。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
# displays a warning dialog box
messagebox.showwarning('warning','this is warning message by tkinter')
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

showwarning()对话框

我们将使用以下对话框中的 askyesno() 消息框在弹出窗口中设置是/否按钮。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
# displays yes/no dialog box
messagebox.askyesno('asking','do you love delftstack?')
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

askyesno() 对话框

askokcancel() 对话框帮助我们做出两个决定,要么是 "ok",要么是 "cancel"。在此示例中,我们使用两个条件进行了一些更改。

由于对话框通过用户操作返回一个 true 或 false 值,我们可以在对话框上设置条件。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
# displays askokcancel dialog box
if messagebox.askokcancel('askokcancel','update your tkinter version') == true:
    print("ok")
else:
    print("cancel")
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

askokcancel() 对话框

askretrycancel() 对话框要求我们重试该过程。icon 参数帮助我们设置任何图标。

在我们的例子中,我们只是设置了一个错误图标。

代码:

from tkinter import *
from tkinter import messagebox
gui=tk()
gui.eval('tk::placewindow %s center' % gui.winfo_toplevel())
gui.withdraw()
# displays askretrycancel dialog box
messagebox.askretrycancel('retry','invalid pin please retry',icon="error")
gui.deiconify()
gui.destroy()
gui.quit()

输出结果:

askretrycancel()对话框

查看笔记

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