Python Forum
How to break a loop in this case?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to break a loop in this case?
#6
I changed your code a bit.
Does marketloop require access to key events?
If it's the case, code needs to be changed. Again.

import pyautogui as pg
import time
import keyboard
import random
import win32api, win32con
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode


class BuffItems(threading.Thread):
    def __init__(self):
        super().__init__()
        self.running = False
        self.pause = False
 
    def start(self):
        """
        Duplicates the start method of threading.Thread
        """
        if not self.running:
            self.running = True
            super().start()

    def stop(self):
        self.running = False

    def pause_toggle(self):
        self.pause = not self.pause
 
    def run(self):
        while self.running:
            time.sleep(0.5) # to reduce speed
            if not self.pause:
                marketloop()


class KeyboardControl:
    start_stop_key = KeyCode(char='s')
    exit_key = KeyCode(char='q')

    def __init__(self):
        self.listener = Listener(on_press=self.on_press)
        self.listener.start()
        self.buff_thread = BuffItems()
        self.buff_thread.start()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.buff_thread.stop()
        self.listener.stop()
        
    def on_press(self, key):
        if key == self.start_stop_key and self.buff_thread.running:
            self.buff_thread.pause_toggle()
        elif key == self.exit_key and self.buff_thread.running:
            self.buff_thread.stop()
            self.listener.stop()

    def join(self):
        while self.buff_thread.running:
            time.sleep(0.1)


def marketloop():
    print(time.time())


def main():
    try:
        with KeyboardControl() as control:
            control.join()
    except KeyboardInterrupt:
        print("\nReceived SIGINT. Exitting programm.")


if __name__ == "__main__":
    main()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
How to break a loop in this case? - by Blainexi - Sep-24-2020, 05:01 AM
RE: How to break a loop in this case? - by finndude - Sep-24-2020, 05:19 AM
RE: How to break a loop in this case? - by Blainexi - Sep-24-2020, 05:39 AM
RE: How to break a loop in this case? - by Blainexi - Sep-24-2020, 10:14 AM
RE: How to break a loop in this case? - by DeaD_EyE - Sep-24-2020, 12:06 PM
RE: How to break a loop in this case? - by Blainexi - Sep-24-2020, 12:19 PM
RE: How to break a loop in this case? - by DeaD_EyE - Sep-24-2020, 12:34 PM
RE: How to break a loop in this case? - by Blainexi - Sep-24-2020, 02:40 PM
RE: How to break a loop in this case? - by Blainexi - Sep-24-2020, 04:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code won't break While loop or go back to the input? MrKnd94 2 1,113 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,959 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  break out of for loop? User3000 3 1,588 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,848 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  Switch case or match case? Frankduc 9 4,887 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  tkinter control break a while loop samtal 0 2,500 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 4,179 Feb-17-2021, 01:03 AM
Last Post: tester_V
  how to break the loop? bntayfur 8 3,237 Jun-07-2020, 11:07 PM
Last Post: bntayfur
  break for loop Agusben 1 2,001 Apr-01-2020, 05:07 PM
Last Post: Larz60+
  Help For AutoGui Loop break ahmetnwpal 0 2,069 Mar-11-2020, 01:14 PM
Last Post: ahmetnwpal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020