[英]How Do I Loop An Input In Python?
I have been searching and searching on how to figure out how to make an input or something go into a while loop. 我一直在搜索和搜索如何找出如何输入或进入while循环的东西。 As in, the input() command won't stop my stopwatch.
同样,input()命令不会停止我的秒表。 I have tried tkinter, pygame, and a couple other methods, but they just didn't work.
我尝试过tkinter,pygame和其他几种方法,但它们只是没有用。 If anyone can help me out, I would prefer something small and simple, if that's even possible.
如果有人可以帮助我,我会更喜欢小而简单的东西,如果有可能的话。 And to be specific on what I want to learn to do, is basically allowing, when any key is pressed, for it to instantly stop (preferably without hitting enter).
并且要具体说明我想要学习的内容,基本上允许在按下任何键时立即停止(最好不要输入)。 Thanks, saddlepiggy!
谢谢,saddlepiggy!
Here is what I have so far, with nothing to activate the stopping: 这是我到目前为止所没有激活停止的东西:
#Setup (Variables and stuff)
hours = 0
minutes = 0
seconds = 0
import time
#Main Part of Code
print("Welcome to PyWatch, a stopwatch coded in Python!")
print("Press any key to start the stopwatch.")
print("Then, press any key to stop it!")
start = input("")
while hours < 48:
seconds = seconds + 1
time.sleep(1)
print(hours, "hours,", minutes, "minutes,", seconds, "seconds")
#If Statements for getting seconds/minutes/hours
if (seconds == 60):
minutes = minutes + 1
seconds = seconds - 60
if (minutes == 60):
hours =hours + 1
minutes = minutes - 60
Threading is what you want. 线程是你想要的。
Create a second thread that waits for the input while your first thread handles your stopwatch code. 创建第二个线程,在第一个线程处理秒表代码时等待输入。 Behold:
看吧:
import threading, sys
def halt():
raw_input()
threading.Thread(target=halt).start()
while hours < 48 and threading.active_count() > 1:
seconds = seconds + 1
time.sleep(1)
# copy and past what you had before
Allow me to elaborate on what's happening: Thus far, all of the code you've written has been single threaded. 请允许我详细说明发生了什么:到目前为止,您编写的所有代码都是单线程的。 This means only one line of code is executed at a time, there's only one thread of execution .
这意味着一次只执行一行代码,只有一个执行线程 。 Consequentially, your script can't multitask, it can't wait for input and print the time at the same time.
因此,您的脚本不能执行多任务,它不能等待输入并同时打印时间。
So when this line is evaluated 所以评估这条线时
threading.Thread(target=halt).start()
The main thread creates a second thread of execution. 主线程创建第二个执行线程。 Meanwhile, the main thread goes on and enters the while loop.
同时,主线程继续进入while循环。 The target parameter is the entry point of the thread, it's the starting point.
目标参数是线程的入口点,它是起点。 It's analogous to
if __name__ == "__main__:"
for the main thread. 它与主线程的
if __name__ == "__main__:"
类似。 Just as the main thread terminates when it reaches the end of if __name__ == "__main__:"
, our second thread will terminate once it reaches the end of halt()
. 就像主线程到达
if __name__ == "__main__:"
结束时一样,我们的第二个线程一旦到达halt()
结束就会终止。
The threading.active_count
function tells you how many threads in are current in execution. threading.active_count
函数告诉您当前执行的线程数。
You can't do that in Python. 你不能用Python做到这一点。 You are asking for a keyboard event.
你在要求键盘事件。 Keyboard events are shipped with a GUI.
键盘事件随GUI一起提供。 This thread explains pretty much everything: QKeyPress event in PyQt
这个帖子解释了几乎所有东西: PyQt中的QKeyPress事件
Or use an external application for your OS that can append output to a file that is read by your Python program in a loop. 或者为您的操作系统使用外部应用程序,该应用程序可以将输出附加到Python程序循环读取的文件中。 When a certain event is detected you can perform some actions.
检测到某个事件时,您可以执行某些操作。 For Linux this tread explains: https://superuser.com/questions/248517/show-keys-pressed-in-linux
对于Linux,这个步骤解释了: https : //superuser.com/questions/248517/show-keys-pressed-in-linux
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.