如何在用户输入之前进行多线程等待用户输入并执行其他操作

时间:2019-04-14 04:35:33

标签: python-3.x

我试图基于命令行创建一个简单的时区仪表板。当按下空格键时,该仪表板允许用户切换到“添加或删除时区”页面以添加或删除时区。我计划每秒在屏幕顶部刷新当前系统时间。我发现了一些使用ctypes控制游标的方法,这非常有用。

但是,在程序切换到“添加或删除时区”页面之后,如何在等待用户完成输入时刷新最长时间?由于“输入”需要等待用户输入,因此似乎没有办法。 (但是C ++的Qt可以对用户界面进行多线程刷新,因此我认为Python也应该能够执行类似的操作。)

由于《 Python崩溃课程:基于项目的动手操作入门》一书没有涵盖多线程编程,因此我搜索了一些文章和案例并修改了代码。

以下是我编写的代码(我对多线程不是很熟悉,多线程代码是我复制的其他人的代码)。

这不是完整的代码,只是测试。

import ctypes
import time
import threading
class COORD(ctypes.Structure):
    _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

STD_OUTPUT_HANDLE= -11
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
dwCursorPosition = COORD()
dwCursorPosition.X = 3
dwCursorPosition.Y = 3
ctypes.windll.kernel32.SetConsoleCursorPosition(std_out_handle,dwCursorPosition)
i=1
class waiting(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.thread_stop = False
        global dwCursorPosition
        dwCursorPosition.X = 1
        dwCursorPosition.Y = 1
        ctypes.windll.kernel32.SetConsoleCursorPosition(std_out_handle,dwCursorPosition)
        s = input("Hi!Input Something!")
    def stop(self):
        self.thread_stop = True
class refresh(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.thread_stop = False
        global dwCursorPosition,i
        dwCursorPosition.X = 3
        dwCursorPosition.Y = 3
        ctypes.windll.kernel32.SetConsoleCursorPosition(std_out_handle,dwCursorPosition)
        i += 1
        print("Refresh!!{}".format(i))
    def stop(self):
        self.thread_stop = True

while True:
    thread1 = refresh()
    thread2 = waiting()
    thread1.start()

    thread2.start()
    thread1.stop()
    thread2.stop()

我希望在等待输入时刷新“刷新”之后的数字。但这似乎不起作用。

0 个答案:

没有答案
相关问题