2

I wrote a code for a hack in a game cold Boxing Simulator 2 in python 3.8.1 with PySimpleGui 4.15.1, and if I try to Click the start/stop button, the gui Freezes but the script connected with the Start/Stop button still runs. But if i change the script connected to the Start/Stop button To something like Print('Chicken') then it works totally perfect‫‫‫‫‫‫‫‫‫‫‫

Here is the Code:

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)
  • This exact same question was already posted. The inner while loop never exits. Unless you make GUI calls, it will appear as if the program freezes – Mike from PSG Jan 16 '20 at 03:06

2 Answers2

5

You can't do really long running tasks in a GUI without some kind of refresh or read operation.

I answered the previous post that was identical to this one, pointing out the problem as being the inner while loop that never exits.

If you must have the inner loop executing forever, then you'll at least need to refresh the GUI. You can do this by calling window.refresh as shown below.

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()

You should never all sleep from within the PySimpleGUI event loop.

The better approach would be to call window.read(timeout=400) instead of time.sleep(0.4). This will keep the OS from thinking your program has hung.


[ EDIT July 2022 ]

A reminder for readers that PySimpleGUI, like many projects, is alive and continuously evolving. Answers found here on StackOverflow are particularly dangerous as they never "die"... nor have "Closed" status... or a designation "No longer recommended"

Some answers provided years ago about PySimpleGUI are not at all the recommended way of doing the same operation now. So much has changed... PySimpleGUI Windows can be much more dynamic, there's an entire set of APIs for handling subprocesses, another new set of APIs helps in managing JSON and INI files, and another is threading support.

For this problem, I would no longer recommend a "polling" solution like shown below. Now I would start a thread and have the window.read() call no longer have a timeout. Polling is a very very wasteful architecture. If possible, don't use a timeout... instead rely on an event happening when something happens. In embedded-system-speak this is called being "interrupt-driven" and is a better solution over "polling" for numerous reasons.

The methods you'll want to research are:|

  • Window.start_thread
  • Window.window.write_event_value
Mike from PSG
  • 5,312
  • 21
  • 39
1

##imports you need to add ##

import threading

create thread to the func is freeze

thread1 = threading.Thread(target= your_func)

call the thread from your loop

thread1.start()

thet solve problem for me GL

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '22 at 10:23
  • BTW it create for me new problem wan i wont to stop the tread is or going to excption or treade end if any one heve idea how to fix it with clean exit i will be vary tankfull – יוגב שושן Jul 28 '22 at 14:08
  • link were i find good and smple to anderstnd information https://superfastpython.com/threading-in-python/#Thread_vs_Process – יוגב שושן Jul 28 '22 at 14:19
  • When you add an answer to an old question with an accepted answer, please make sure you are contributing an improvement on existing answers, or a new approach, or a better explanation. – rachwa Jul 31 '22 at 12:25