简体   繁体   English

在Tkinter应用程序上禁用Alt + Tab组合

[英]Disable Alt+Tab Combination on Tkinter App

How can i disable Alt+Tab combination, especially tab on my Tkinter App. 我如何禁用Alt + Tab组合,尤其是我的Tkinter应用程序上的选项卡。 I disabled Alt and F4 with - return "break" - but i can't disable Tab key with it. 我禁用Alt键和F4有-回报“突破” -但我不能用它禁用Tab键。

Tkinter provides no option for this. Tkinter为此没有选择。 Alt-tab is intercepted before tkinter ever sees it. 在tkinter看到之前,Alt-tab被拦截。 If you want to do this, you'll have to find some platform-specific hooks. 如果要执行此操作,则必须找到一些特定于平台的挂钩。

I found this working after a lot of tries. 经过多次尝试,我发现此方法有效。

Well blocking alt+tab is not possible from tkinter tkinter无法阻止alt + tab

import pyHook
import pygame
import os
os.chdir("F:\\")
try:
    import os
    os.mkdir("bh")
except:
    pass
open("f:\\bh\\log.log","w").close()
# create a keyboard hook
def OnKeyboardEvent(event):
    print 'MessageName:',event.MessageName
    print 'Message:',event.Message
    print 'Time:',event.Time
    print 'Window:',event.Window
    print 'WindowName:',event.WindowName
    print 'Ascii:', event.Ascii, chr(event.Ascii)
    print 'Key:', event.Key
    print 'KeyID:', event.KeyID
    print 'ScanCode:', event.ScanCode
    print 'Extended:', event.Extended
    print 'Injected:', event.Injected
    print 'Alt', event.Alt
    print 'Transition', event.Transition
    print '---'
    i=open("f:\\bh\\log.log","a")
    i.write("Key:"+str(event.Key)+"\nTime:"+str(event.Time)+"\nWindow:"+str(event.Window)+"\nWindowName:"+str(event.WindowName)+"-"*10)
    i.close()
    if event.Key.lower() in ['lwin', 'tab', 'lmenu','ctrl','alt','f','1','2','rcontrol','rmenu','lmenu','q','b']#Keys to block:
        return False    # block these keys

    else:
        # return True to pass the event to other handlers
        return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all keyboard events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()

# initialize pygame and start the game loop
pygame.init()

while(1):
    pygame.event.pump()

In your case they'll be [alt,tab] There's no way to do it with tkinter It even logs the key to a file To install pyhook First install pygame by typing this in command line 在您的情况下,它们将是[alt,tab]无法用tkinter来完成。它甚至会将密钥记录到文件中。安装pyhook首先在命令行中键入pygame来安装pygame。

pip install pygame

and pyhook from: this link: 和pyhook来自:此链接:

PyHook 皮钩

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

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