【问题标题】:why does the keyboard.is_pressed() function refuse to work when pressed?为什么keyboard.is_pressed() 函数在按下时拒绝工作?
【发布时间】:2022-07-19 02:58:02
【问题描述】:

我有不到 3 个月的 Python 编程经验,但基本上,我有一个程序可以无限期地从 CoinGecko API 中提取值并创建进程,以便提取数据的函数可以彼此独立运行,但 id 喜欢在我按下设置为“q”的指定热键后,它会中断循环。每当我按下热键时,什么都没有发生,循环只是继续运行。我试过使用keyboard.read_key() 函数,但这只会停止我的程序运行,直到我按下 q 按钮,这会导致程序运行一次循环然后关闭。我不知道为什么is_pressed() 函数拒绝工作并且我想得到更高级人员的帮助

有问题的代码段:

from multiprocessing.dummy import freeze_support                
from pycoingecko import CoinGeckoAPI
import time
from multiprocessing import Process
from multiprocessing import Pool
import multiprocessing
import keyboard as kb
import psutil




cg = CoinGeckoAPI()


class CGCoin:
    def __init__(self, coinname, coinid):
        self.coinname = coinname
        self.coinid = coinid
   

    def pulldata(self):
       
        while True:
             
            wishtoquit = False
            if kb.is_pressed('Q'):
                print('ending after this loop')
                wishtoquit = True
                
            
            timestarted = time.asctime()
            
            self.prices = []
            self.daychanges = []
            self.volumes = []
            self.marketcaps = []
            self.weekchanges = []
            self.highs = []
            self.lows = []
            self.times = []
            print(f'starting {self.coinname} reading at {timestarted}')
            loops = 0 
            maxloops = 2
            while loops < maxloops:
                
                time.sleep(15)
                coin = cg.get_coin_by_id(f'{self.coinid}')
                time.sleep(5)
                coinvalues = coin.get('market_data')
                coinprices = coinvalues.get('current_price')
                coinvolumes = coinvalues.get('total_volume')
                mrktcaps = coinvalues.get('market_cap')
                dayhigh = coinvalues.get('high_24h')
                daylow = coinvalues.get('low_24h')
                daychangepercentage = coinvalues.get('price_change_percentage_24h')
                weekchangepercentage = coinvalues.get('price_change_percentage_7d')
                coinprice = coinprices.get('usd') 
                coinvolume = coinvolumes.get('usd')
                coincap = mrktcaps.get('usd')
                coindayhigh = dayhigh.get('usd')
                coindaylow = daylow.get('usd')
                timepulled = time.asctime()


                self.prices.append(coinprice)
                self.daychanges.append(daychangepercentage)
                self.volumes.append(coinvolume)
                self.marketcaps.append(coincap)
                self.weekchanges.append(weekchangepercentage)
                self.highs.append(coindayhigh)
                self.lows.append(coindaylow)
                self.times.append(timepulled)
                loops = loops + 1 
                print(loops)
            timeended = time.asctime()



            })
            print(f'stopping {self.coinname} reading at {timeended}')
            if wishtoquit:
                print('ending loops')
                
                break

            time.sleep(5)

           


            
bitcoin = CGCoin('Bitcoin', 'bitcoin')
ethereum = CGCoin('Ethereum', 'ethereum')
if __name__ == '__main__':
    freeze_support()
    btcpul = Process(target=bitcoin.pulldata, name=bitcoin.coinname)
    btcpul.start()
 

如果有人有任何想法或功能齐全的解决方法,我真的很想听听。我非常感谢收到的任何帮助

【问题讨论】:

    标签: python multiprocessing keyboard keyboard-events


    【解决方案1】:

    看起来 PyPi 键盘在 linux 上需要 root 权限。

    您可以只执行 kb.on_press_key("p", lambda _: sys.exit(0)) 并执行 sys.exit(0) 来结束脚本。

    如果您在终端中运行它,您应该能够按ctrl+c 来中断它的执行。

    参考:How to detect key presses?

    【讨论】:

    • ctrl c 有效,但每当我按 p 时,什么都没有发生。它不可能是它需要 root 权限,因为脚本在 Windows 上运行。我开始认为这是我的键盘本身或其他东西的问题。不适使用 ctrl c 并可能稍后再回来或使用不同的模块
    【解决方案2】:

    我没有时间将您导入的所有内容添加到我的计算机中,但是否因为“Q”是大写字母?

    如果 kb.is_pressed('Q'):

    【讨论】:

      猜你喜欢
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多