简体   繁体   English

Tkinter 专注于弹出 window

[英]Tkinter focus on popup window

I have a question regarding a popup window from the main window.我有一个关于来自主 window 的弹出 window 的问题。 How do I ensure that when there's a popup window (to set date & time), the main window cannot be touched (ie closed or press anything) until the user close the popup window.我如何确保当出现弹出窗口 window(设置日期和时间)时,在用户关闭弹出窗口 window 之前,不能触摸主 window(即关闭或按下任何东西)。

I have tried using grab_set but the main window can still be closed which result in error message:我试过使用grab_set ,但主window仍然可以关闭,导致错误消息:

 bgerror failed to handle background error.

grab_set_global work for me but I won't be able to move the popup window around. grab_set_global为我工作,但我无法移动弹出窗口 window。

# Main window
root = Tk()
root.title("Restaurants")
root.geometry("800x500")

lines of codes..... where user will select if they want to set the date 
and time

# Popup window
def date_time():
    popup = Tk()
    popup.title("Set Date and Time")
    popup.geometry("500x500")
    popup.grab_set()  # Not working

    lines of codes to run

I want it to focus on the popup window and the main window under it will not be able to close until the popup window is closed/destroyed.我希望它专注于弹出 window 和它下面的主要 window 将无法关闭,直到弹出 window 关闭/销毁。

You can use popup.focus_force , but probably first check if root is in focus.您可以使用popup.focus_force ,但可能首先检查 root 是否处于焦点。 But that seems to be similar to cheating.但这似乎类似于作弊。

Ok, I have managed to solve my problem by changing the popup = Tk() to popup = Toplevel() and popup.grab_set works on the popup window.好的,我已经设法通过将popup = Tk()更改为popup = Toplevel()来解决我的问题,并且popup.grab_set适用于弹出窗口 window。 The main window can't be touched till the popup window is closed.在弹出 window 关闭之前,无法触摸主 window。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM