PySimpleGUI 冻结
PySimpleGUI Freezes
我在 python 3.8.1 中使用 PySimpleGui 4.15.1 编写了一个游戏冷拳击模拟器 2 的破解代码,如果我尝试单击 start/stop 按钮,gui冻结,但与 Start/Stop 按钮连接的脚本仍在运行。但是,如果我将连接到 Start/Stop 按钮的脚本更改为 Print('Chicken') 之类的东西,那么它就完全完美了
代码如下:
import PySimpleGUI as sg
import ctypes
import time
import pynput
sg.theme('DarkBrown1')
layout = [ [sg.Text('BOXING SIMULATOR 2 HACK', size=(20, 2), justification='center')],
[sg.Text('', size=(10, 2), font=('Helvetica', 20), justification='center', key='_OUTPUT_')],
[sg.T(' ' * 5), sg.Button('Start/Stop', focus=True), sg.Quit()]]
window = sg.Window('BOXING SIMULATOR 2 HACK', layout)
SendInput = ctypes.windll.user32.SendInput
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time",ctypes.c_ulong),
("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
def PressKeyPynput(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = pynput._util.win32.INPUT_union()
ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKeyPynput(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = pynput._util.win32.INPUT_union()
ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
HACK_running, counter = False, 0
while True:
event, values = window.read(timeout=10)
if event in (None, 'Quit'):
break
elif event == 'Start/Stop':
HACK_running = not HACK_running
if HACK_running:
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
PressKeyPynput(0x11)
time.sleep(0.5)
ReleaseKeyPynput(0x11)
PressKeyPynput(0x1F)
time.sleep(0.6)
ReleaseKeyPynput(0x1F)
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
time.sleep(300)
如果没有某种刷新或读取操作,您无法在 GUI 中执行很长的 运行 任务。
我回答了上一个 post 与这个相同的问题,指出问题是永远不会退出的内部 while 循环。
如果您必须让内部循环永远执行,那么您至少需要刷新 GUI。您可以通过调用 window.refresh
来完成此操作,如下所示。
while True:
event, values = window.read(timeout=10)
if event in (None, 'Quit'):
break
elif event == 'Start/Stop':
HACK_running = not HACK_running
if HACK_running:
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
PressKeyPynput(0x11)
time.sleep(0.5)
ReleaseKeyPynput(0x11)
PressKeyPynput(0x1F)
time.sleep(0.6)
ReleaseKeyPynput(0x1F)
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
time.sleep(300)
window.refresh()
你永远不应该在 PySimpleGUI 事件循环中休眠。
更好的方法是调用 window.read(timeout=400)
而不是 time.sleep(0.4)
。这将使 OS 不会认为您的程序已挂起。
我在 python 3.8.1 中使用 PySimpleGui 4.15.1 编写了一个游戏冷拳击模拟器 2 的破解代码,如果我尝试单击 start/stop 按钮,gui冻结,但与 Start/Stop 按钮连接的脚本仍在运行。但是,如果我将连接到 Start/Stop 按钮的脚本更改为 Print('Chicken') 之类的东西,那么它就完全完美了
代码如下:
import PySimpleGUI as sg
import ctypes
import time
import pynput
sg.theme('DarkBrown1')
layout = [ [sg.Text('BOXING SIMULATOR 2 HACK', size=(20, 2), justification='center')],
[sg.Text('', size=(10, 2), font=('Helvetica', 20), justification='center', key='_OUTPUT_')],
[sg.T(' ' * 5), sg.Button('Start/Stop', focus=True), sg.Quit()]]
window = sg.Window('BOXING SIMULATOR 2 HACK', layout)
SendInput = ctypes.windll.user32.SendInput
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time",ctypes.c_ulong),
("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
def PressKeyPynput(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = pynput._util.win32.INPUT_union()
ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKeyPynput(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = pynput._util.win32.INPUT_union()
ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
HACK_running, counter = False, 0
while True:
event, values = window.read(timeout=10)
if event in (None, 'Quit'):
break
elif event == 'Start/Stop':
HACK_running = not HACK_running
if HACK_running:
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
PressKeyPynput(0x11)
time.sleep(0.5)
ReleaseKeyPynput(0x11)
PressKeyPynput(0x1F)
time.sleep(0.6)
ReleaseKeyPynput(0x1F)
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
time.sleep(300)
如果没有某种刷新或读取操作,您无法在 GUI 中执行很长的 运行 任务。
我回答了上一个 post 与这个相同的问题,指出问题是永远不会退出的内部 while 循环。
如果您必须让内部循环永远执行,那么您至少需要刷新 GUI。您可以通过调用 window.refresh
来完成此操作,如下所示。
while True:
event, values = window.read(timeout=10)
if event in (None, 'Quit'):
break
elif event == 'Start/Stop':
HACK_running = not HACK_running
if HACK_running:
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
PressKeyPynput(0x11)
time.sleep(0.5)
ReleaseKeyPynput(0x11)
PressKeyPynput(0x1F)
time.sleep(0.6)
ReleaseKeyPynput(0x1F)
PressKeyPynput(0x02)
time.sleep(0.08)
ReleaseKeyPynput(0x02)
time.sleep(300)
window.refresh()
你永远不应该在 PySimpleGUI 事件循环中休眠。
更好的方法是调用 window.read(timeout=400)
而不是 time.sleep(0.4)
。这将使 OS 不会认为您的程序已挂起。