0

I am coding a sign in and I would like there to be no way out unless signin is complete. But in order to do that I need to disable the Windows key so they cannot leave the window. I is has no header, I disabled the WM delete window protocol, I have automatic full screen. I also need to make it dynamically set itself to the middle but I am not that far. Messagebox import works.

My Code:

from tkinter import *
import messagebox
from win32api import GetSystemMetrics

def getfullscreensize():
    global width
    width = GetSystemMetrics(0)
    global height
    height = GetSystemMetrics(1)
def donothing():
    pass

root = Tk()
root.attributes('-fullscreen', True)
root.protocol("WM_DELETE_WINDOW", donothing)
root.overrideredirect(1)
root.bind("<key>", lambda e: "break")
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
ixonblitz
  • 3
  • 2
  • 1
    Not related to your question but there is a way to get the screen width/height using tkinter: `root.winfo_screenwidth()` and `root.winfo_screenheight()` – TheLizzard Jul 18 '21 at 16:54
  • 1
    I suspect windows defender would block any attempt to disable the Windows Key or Start Menu. Why is this feature necessary? – Ben Alan Jul 18 '21 at 16:57
  • If you use `root.attributes("-topmost", True)`, it will force the window to go to the top and stay there. That way even if the user opens a program, that program wouldn't be visible. – TheLizzard Jul 18 '21 at 17:00
  • 1
    @TheLizzard The start menu still appears above the window, as does the taskbar – Henry Jul 18 '21 at 17:07
  • @ixonblitz Looking at [this](https://stackoverflow.com/a/4529615/11106801), disabling the window key is very hard and requires using low level techniques. And you can't disable `Ctrl-Alt-Delete`. – TheLizzard Jul 18 '21 at 17:09
  • @Henry Never said that it wouldn't. I just pointed out that using `root.attributes("-topmost", True)` is good enough most of the time as it makes it hard for the user to interact other applications. – TheLizzard Jul 18 '21 at 17:10
  • @TheLizzard to add to You it is even possible to have an `.after()` loop that forces focus to that window, that way it is even harder to do anything – Matiiss Jul 18 '21 at 17:16
  • @Matiiss Tried. Doesn't work for me :-(. I even tried `root.focus_force()` in a loop that ran every 200 ms. – TheLizzard Jul 18 '21 at 17:18
  • 1
    The OS (correctly!) will try it's best to prevent you from doing this. Any workaround you find will certainty trigger any Anti virus. Don't do it. – MegaIng Jul 18 '21 at 18:19
  • `keyboard` sends information to system and it decides what to do with key - open menu or send it to active window (tkinter). So tkinter can't control it. You have to block it on system level. – furas Jul 18 '21 at 22:46

2 Answers2

2

How to disable Windows key using tkinter?

In short, you can't. Tkinter does not have any features that allow it to disable OS-level features like the windows key.

In order to disable this key you'll have to find some other platform-specific solution.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 3
    There is a cross-platform method (although the key exists only on windows), it is not programmatical but it should work. That would be physically destroying that key, and I don't mean just the key cap, I mean take a drill and drill that key out, if one wants they can even code some arduino thingy to do it (to make it more programmatical). That should disable that key for sure, at least from user's perspective. – Matiiss Jul 18 '21 at 17:30
0

you can use import keyboard keyboard.block_key('Win')