我想在主窗口上制作一个弹出窗口
Posted
技术标签:
【中文标题】我想在主窗口上制作一个弹出窗口【英文标题】:I Want to make a Pop up window over main window 【发布时间】:2022-01-01 21:38:52 【问题描述】:它应该表现得像当弹出窗口打开时我们不能使用主窗口,直到我们退出弹出窗口
我不使用 tkinter 消息框的原因是我无法自定义它
我想自定义并找到了一种方法,但问题是当弹出窗口打开时,我可以轻松单击主窗口按钮并打开多个弹出窗口,所以我希望在窗口弹出时禁用主窗口并获取弹窗关闭后恢复正常
我的代码是这样的
from tkinter import *
win = Tk()
win.title("Main Window")
def closing_try():
close = Tk()
close.title("Warning")
close.geometry('200x100')
close.overrideredirect(0)
close.configure(bg='white')
close.resizable(False, False)
close.attributes("-topmost", True)
close.eval('tk::PlaceWindow . center')
def save_b_func():
close.destroy()
save_func()
win.destroy()
def dont_save_b_func():
win.destroy()
close.destroy()
def cancel_b_func():
close.destroy()
ask_lbl = Label(close, text='''Do You Want to Save File
Before Exiting?''', bg='white', font=('consolas', 10, 'normal'))
ask_lbl.place(x=10, y=5)
save_b = Button(close, text='Save', font=('consolas', 10, 'normal'), command=save_b_func, bg='white').place(x=9, y=50)
dont_save_b = Button(close, text="Don't Save", font=('consolas', 10, 'normal'), command=dont_save_b_func, bg='white').place(x=53, y=50)
cancel_b = Button(close, text='Cancel', font=('consolas', 10, 'normal'), command=cancel_b_func, bg='white').place(x=139, y=50)
close.mainloop()
button = Button(win, text='click me', command = closing_try).pack()
win.mainloop()
【问题讨论】:
【参考方案1】:如果你想让主窗口可见,你可以这样做:
win.grab_set() # show popup window
win.grab_release() # return to normal
或
master.withdraw() # hide main window
master.deiconify()# return main window
这里有一些示例代码,以便您了解它的工作原理:
from tkinter import *
win = Tk()
win.geometry("450x400")
new_win = Toplevel(win)
new_win.geometry("700x250")
new_win.title("NEW WINDOW")
new_win.withdraw()
Label(new_win, text="Example Label", font=('Helvetica 15 bold')).pack(pady=10)
def show_new():
new_win.grab_set()
new_win.deiconify()
def show_old():
new_win.grab_release()
new_win.withdraw()
def print_text():
print("BUTTON IS PRESSED")
button = Button(win, text="Show",command= show_new)
button.pack(pady=50)
button_text = Button(win, text="print",command=print_text)
button_text.pack(pady=100)
button2 = Button(new_win, text="OK",command= show_old)
button2.pack(pady=50)
win.mainloop()
【讨论】:
以上是关于我想在主窗口上制作一个弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章