Looping until a specific key is pressed [duplicate]
2024/11/16 13:40:24
I was trying to make a while loop which would stop running when a specific key is pressed. The problem is that the loop runs infinitely. My loop:
import time
import keyboardwhile (not keyboard.is_pressed("esc")):print("in loop...")time.sleep(2)
I am using the keyboard module. What is wrong with my loop and how can I fix it?
(I don't really want to use a Repeat-until or equivalent loop in Python thing in this case.)
Answer
Here is a solution which works:
import keyboard
import time
import threadingclass main:def __init__(self):# Create a run variableself.run = True# Start main thread and the break threadself.mainThread = threading.Thread(target=self.main)self.breakThread = threading.Thread(target=self.breakThread)self.mainThread.start()self.breakThread.start()def breakThread(self):print('Break thread runs')# Check if run = Truewhile True and self.run == True:if keyboard.is_pressed('esc'):self.newFunction()def main(self):print('Main thread runs')# Also check if run = True while not keyboard.is_pressed('esc') and self.run:print('test')time.sleep(2)# Break like thisif keyboard.is_pressed('esc'):breakprint('test')time.sleep(2)def newFunction(self):self.run = Falseprint('You are in the new function!')program = main()
I want to have my python program generate visio drawings using shapes from a stencil (.vss) file. How can I do that? I think I could generate the xml formatted .vdx file, but there isnt a lot of docum…
I am working on Real Time Object Detection using YOLOv3 with OpenCV and Python. Its works well. Currently I try to capture detected image of object and display in flask. Do someone know how to implemen…
I have to use Tensorflow 0.11 for this code repo and this is the error I get:(py35) E:\opensource_codes\gesture_recognition\hand3d-master>python run.py
Traceback (most recent call last):File "r…
Id like to add subtotal rows for index #1 (ie. Fruits and Animal) and subtotal columns for columns (ie. 2015 and 2016).For the subtotal columns, I could do something like this, but it seems inefficient…
I have a dataframe, df, that contains two columns that contain quarter values. I would like to create two more columns that are the equivalent "long dates".
Data
ID Quarter Delivery
A …
i have already tried these but by this we can only get hour/minute/second but not both minute and second
In[7]:
df[B] = df[A].dt.components[hours]
df
Out[7]:A B0 02:00:00 2
1 01:00:00 1from this tim…
Please help me understand how the lines below are working.How does a pair of parentheses create an array and then individual
elements go through logical condition check and create a new array?
How doe…
When I try to scrape roster links, I get https://gwsports.com/roster.aspx?path=wpolo when I open it on chrome it changes to https://gwsports.com/sports/mens-water-polo/roster. I want to scrape it in p…
If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_title and that seems to h…