未选择时如何允许Tkinter GUI检测按键

时间:2019-07-17 18:01:55

标签: python python-3.x tkinter keypress

我正在尝试制作完整的GUI,因为《 A游戏禁地2》仅在选中了tkinter框(焦点对准)时才检测到我的按键操作。在游戏中这是没有发生的,因此我需要一种方法让tkinter在未选择时检测我的按键。 到目前为止,这是代码。尚未完成,请忽略“自动执行”和“自动退出”,问题是“自动重新加载”。单击一次将其打开,然后再次将其关闭。只需为武器插槽选择任何数字(1-4),不会影响该错误。

from easygui import *
from pyautogui import *
from time import *
import os
from tkinter import *
count = 1
slot = "0"
allowReload, allowExit = False, False
def poop():
    sleep(3)
    print("poop")
def countdown(count):
    autoexecB.config(text = count)
    if count == 2:
        autoexecB.config(bg = "orange")
    if count == 1:
        autoexecB.config(bg = "yellow")
    if count == 0:
        autoexecB.config(text = "Running...", bg = "#44ff00")
    if count > -1:
        root.after(1000, countdown, count-1)
    else:
        for i in range(1, 3):
            sleep(1)
            press("'")
            sleep(0.5)
            type("exec Patch.txt")
            press("enter")
            press("esc")
            press("enter")
            sleep(4)
            press("esc")
            pressMult("down", 4)
            press("enter")
            press("up")
            press("enter")
        sleep(1)
        press("'")
        sleep(0.5)
        type("exec STV.txt")
        press("enter")
        press("esc")
        autoexecB.config(text = "Auto Exec", bg = "red")
def type(text):
    typewrite(text)
def pressMult(key, amount):
    for i in range(1, amount+1):
        press(key)
def autoexec():
    countdown(3)
def info():
    msgbox("Auto Exec: Runs Mods automaticly\nAuto Exit: Exits the game automaticly to the main menu using INSERT\nAuto Reload: Automaticly reloads your gun using LEFT SHIFT")
def exit():
    global count
    count = 1
    press("esc")
    pressMult("down", 4)
    press("enter")
    press("up")
    press("enter")
    sleep(2.1)
    press("enter")
    if choose == "FARM at Hero's Pass":
        sleep(3)
        keyDown("w")
        sleep(0.7)
        keyUp("w")
        keyDown("d")
        sleep(1)
        keyUp("d")
        keyDown("ctrl")
        sleep(0.5)
        press("e")
        keyUp("ctrl")
        count += 1
def reloadslot():
    global allowReload, slot
    while True:
        if allowReload == True:
            break
        slot = str(integerbox("Enter in the weapon's slot to be reloaded"))
        if slot not in ("1","2","3","4"):
            msgbox("A weapon can only be in slot 1, 2, 3 or 4")
        else:
            break
    if allowReload == True:
        allowReload = False
    else:
        allowReload = True
def on_press(event):
    print(event.keysym)
    if event.keysym == "Insert" and allowExit == True:
        print("exit")
        exit()
    if event.keysym == "Shift_L" and allowReload == True:
        print("running reload")
        press(";")
        press("e")
        press(slot)
    print("done")
root = Tk()
root.bind('<KeyPress>', on_press)
root.geometry("378x134")
root.config(bg = "blue")
autoexecB = Button(text = "Auto Exec", bg = "red", font = ("calibri","13"), height = 3, width = 13, command = lambda: autoexec())
autoexitB = Button(text = "Auto Exit", bg = "red", font = ("calibri","13"), height = 3, width = 13)
autoreloadB = Button(text = "Auto Reload", bg = "red", font = ("calibri","13"), height = 3, width = 13, command = lambda: reloadslot())
infoB = Button(text = "INFO", bg = "blue", width = 26, height = 3, command = lambda: info())
exitB = Button(text = "EXIT", bg = "blue", width = 25, height = 3, command = lambda: root.destroy())
autoexecB.place(x = 0, y = 0)
autoexitB.place(x = 126, y = 0)
autoreloadB.place(x = 252, y = 0)
infoB.place(x = 0, y = 78)
exitB.place(x = 193, y = 78)
root.mainloop()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

  

我需要一种方法让tkinter在未被选中的情况下检测我的按键操作

您不能为此使用tkinter。仅当具有键盘焦点时,它才能检测键盘事件。您将必须使用一些特定于平台的库来完成所需的工作。