Looping until a specific key is pressed [duplicate]

2024/10/6 18:32:25

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()
https://en.xdnf.cn/q/118922.html

Related Q&A

Getting sub-dataframe after sorting and groupby

I have a dataframe dfas:Election Year Votes Vote % Party Region 0 2000 42289 29.40 Janata Dal (United) A 1 2000 27618 19.20 Rashtriya Ja…

Use .vss stencil file to generate shapes by python code (use .vdx?)

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…

How can I capture detected image of object Yolov3 and display in flask

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…

ValueError: Shapes (2, 1) and () are incompatible

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…

Subtotals for Pandas pivot table index and column

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…

Create two new columns derived from original columns in Python

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 …

In dataframe: how to pull minutes and seconds combinedly(mm:ss) from timedelta using python

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…

Understanding python numpy syntax to mask and filter array

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…

How to get the proper link from a website using python beautifulsoup?

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…

tkinter frame propagate not behaving?

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…