如何创建检查后台键盘输入以重启脚本的功能

时间:2019-06-22 19:01:26

标签: python input background keyboard

我对python编程相当陌生,我做了一个简单的脚本,我想添加一个函数,该函数在后台检查键盘输入(在我的情况下,当您按“ r”时),以及按其时它会重新启动程序或只是转到main()函数(从开始开始再次运行程序)。就是这样,但我只是想不通, 所以我尝试了这三件事:

1.(线程): 因此它创建了两个线程,一个是程序,另一个正在检查键盘输入,但这很奇怪,在我的情况下也不起作用。
2.(多重处理): 我也尝试过使用多处理模块,但是还没有弄清楚。
3.(后台线程): 我也尝试了后台线程代码: (链接:http://sebastiandahlgren.se/2014/06/27/running-a-method-as-a-background-thread-in-python/),并在self.run中对“ r”进行了键盘检测,但也无法正常工作

import threading
import keyboard
import time
from multiprocessing import Process
import os
from threading import Thread
print ("grab piece of paper to write down bmi")
print ("you can restart at any time by pressing (r)")
time.sleep(1)
def main():
    running = True
    while running:
        global height
        height= input("enter height (cm): ")
        # here the code sometimes shows in python shell,
        # just like it stopped, ended (it print out that: >> ),
        # but it still continiues normally
        try:
            val = int(height)
            while True:
                global weight
                weight = input("enter weight: ")
                try:
                    val = int(weight)
                    if True:
                        global name
                        name = input("enter name: ")
                        print (" double check your inputs for the bmi")
                        print (" when you are sure it is correct press c")
                        print (" and if it's not press b")
                        import keyboard
                        a = 1
                        while a == 1:
                            if keyboard.is_pressed("c"):
                                main2()
                            elif keyboard.is_pressed("b"):
                                main()
                            else:
                                continue
                except ValueError:
                    if True:
                        print(" this is not a number (int)")
                        print(" please enter a number")
                        continue
        except ValueError:
            if True:
                print(" this is not a number")
                print(" please enter a number")
                continue


def main2():
    result = (int(weight)) / ((int(height)) / 100)**2
    print ("name: {}".format(name))
    print ("bmi: {}".format(result))
    print ("this person is:")
    if (result) <= 18.5: print("  underweight")
    elif (result) > 18.5 and (result) <= 25: print("  healthy")
    elif (result) > 25 and (result) <= 30: print("  overweight")
    elif (result) > 30 and (result) <= 35: print("  obese")
    elif (result) > 35: print("  you are fat guy!")
    print("after you write bmi press k to stop and quit the program ")
    import keyboard
    a = 1
    while a == 1:
        if keyboard.is_pressed("k"):
            import sys
            sys.exit(0)
        else:
            continue
def keyboard_detect():
    while True:
        if keyboard.is_pressed("r"):
            main()
th1 = Thread(target=main)
th2 = Thread(target=keyboard_detect)
th1.start()
th2.start()

很抱歉,无法粘贴整个代码,但要点是,当我尝试使用更简单的代码时会起作用,但是使用此代码却不想工作, 该程序有时总的来说很奇怪,但是当我按“ r”时,什么也没发生,它甚至不显示错误消息,而且我正在使用python 3.7.2,并且在示例代码中使用可能会出现问题 4个空格和制表符(如果您收到错误:刷新时出现意外的eol)

所以我仍在寻找解决方案。 如果找到解决方案,谢谢**(在python中平方):),但还是要感谢

0 个答案:

没有答案