在 Python TKinter 中创建弹出窗口时禁用底层窗口
我们熟悉弹出窗口,并在许多应用程序中使用它。可以在 Tkinter 应用程序中通过创建 Toplevel(root) 窗口的实例来创建弹出窗口。对于特定应用程序,我们可以在按钮对象上触发弹出窗口。
让我们创建一个 Python 脚本,在显示弹出窗口后关闭底层或主窗口。可以使用 withdraw() 方法在弹出窗口中驻留时关闭主窗口。
示例
通过此示例,我们将创建一个弹出对话框,该对话框可以在单击按钮后触发。弹出窗口打开后,父窗口将自动关闭。
#Import the required library
from tkinter import*
#Create an instance of tkinter frame
win= Tk()
#Define geometry of the window
win.geometry("750x250")
#Define a function to close the popup window
def close_win(top):
top.destroy()
win.destroy()
#Define a function to open the Popup Dialogue
def popupwin():
#withdraw the main window
win.withdraw()
#Create a Toplevel window
top= Toplevel(win)
top.geometry("750x250")
#Create an Entry Widget in the Toplevel window
entry= Entry(top, width= 25)
entry.insert(INSERT, "Enter Your Email ID")
entry.pack()
#Create a Button Widget in the Toplevel Window
button= Button(top, text="Ok", command=lambda:close_win(top))
button.pack(pady=5, side= TOP)
#Create a Label
label= Label(win, text="Click the Button to Open the Popup Dialogue", font= ('Helvetica 15 bold'))
label.pack(pady=20)
#Create a Button
button= Button(win, text= "Click Me!", command= popupwin, font= ('Helvetica 14 bold'))
button.pack(pady=20)
win.mainloop()输出
当我们执行上述代码段时,将显示一个窗口。

现在单击“Click Me”按钮。它将打开一个弹出窗口并关闭父窗口。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP