简体   繁体   English

Python 3-有没有办法将鼠标保留在tkinter窗口中

[英]Python 3 - Is there a way to keep the mouse inside the tkinter window

Python 3 Tkinter Python 3 Tkinter

Hey, I'm looking for something that don't allow the user to leave a window if a variable is true/false. 嘿,我正在寻找某种不允许变量为true / false的情况下用户离开窗口的东西。

basicly, somethings that keeps the mouse inside the tkinter window 基本上,某些东西可以将鼠标保留在tkinter窗口中

You can't force the mouse to stay in the window, though you can "grab" all of the mouse events, preventing the user from clicking anywhere but on your app. 尽管可以“捕获”所有鼠标事件,但不能强迫鼠标停留在窗口中,从而阻止用户单击应用程序以外的任何位置。 This is very dangerous as you can lock up your computer if your code has a bug in it. 这是非常危险的,因为如果代码中有错误,则可以锁定计算机。

See How can I make area outside toplevel unclickable? 请参阅如何使顶级以外的区域不可点击?

Not directly, no. 不直接,不。 However you could monitor the mouse position with the <Motion> event and correct it every time it goes outside. 但是,您可以使用<Motion>事件监视鼠标位置,并在每次进入外部时对其进行更正。 How to correct it would depend on your OS; 如何纠正它取决于您的操作系统。 look into pyautogui for a crossplatform solution. 查看pyautogui中的跨平台解决方案。

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

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