如何获取 Tkinter/Python 中的弹出对话框?


Tkinter 是用于创建和开发基于 GUI 应用程序的标准 Python 库。我们可以在 Tkinter 中创建应用程序,并向其中添加小部件,使应用程序更加交互。

假设我们想在应用程序中显示一个弹出对话框。在这种情况下,我们可以使用 Tkinter 中内置的 messagebox 模块。它允许我们显示各种对话框,例如错误、信息框、确认框等。

示例

在此示例中,我们创建了一个按钮,单击该按钮后将在屏幕上显示一个弹出消息。

# Import the required library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

# Create an instance of tkinter frame
win=Tk()

# Set the geometry
win.geometry("700x250")

# Define a button to show the popup message box
def on_click():
   messagebox.showinfo("Message", "Hey folks!")

# Add a Label widget
Label(win, text="Click the button to open a popup", font=('Georgia 13'))

# Create a button to open the popup dialog
ttk.Button(win, text="Open Popup", command=on_click).pack(pady=30)

win.mainloop()

输出

运行上述代码将显示一个窗口,其中有一个按钮用于打开对话框。

单击按钮以在屏幕上显示弹出对话框。

更新于: 2021 年 8 月 5 日

4K+ 浏览次数

启动您的职业

完成课程即可获得认证

开始
广告
© . All rights reserved.