Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使线程更新的字符串反映Python上的更改';什么是诅咒?_Python_Terminal_Curses - Fatal编程技术网

如何使线程更新的字符串反映Python上的更改';什么是诅咒?

如何使线程更新的字符串反映Python上的更改';什么是诅咒?,python,terminal,curses,Python,Terminal,Curses,我计划将curses库实现到客户机的现有Python脚本中。脚本将完全通过SSH运行 我目前正在尝试模拟脚本将生成的一些输出 在我的“测试水域”脚本中,我有3个变量:x、y、z 我有一个线程在curses循环旁边运行,它每x秒递增x、y和z。在循环中,我只是将这三个变量打印到终端屏幕上 问题:在我提供某种输入之前,变量不会更新。 如何使终端字符串自动更新值? 我正在Kubuntu的一个终端上测试这个。我试过Urwid,遇到了类似的问题 import curses import time from

我计划将curses库实现到客户机的现有Python脚本中。脚本将完全通过SSH运行

我目前正在尝试模拟脚本将生成的一些输出

在我的“测试水域”脚本中,我有3个变量:x、y、z

我有一个线程在curses循环旁边运行,它每x秒递增x、y和z。在循环中,我只是将这三个变量打印到终端屏幕上

问题:在我提供某种输入之前,变量不会更新。 如何使终端字符串自动更新值?

我正在Kubuntu的一个终端上测试这个。我试过Urwid,遇到了类似的问题

import curses
import time
from threading import Thread

x, y, z = 0, 0, 0
go = True


def increment_ints():
    global x, y, z
    while go:
        x += 1
        y += 2
        z += 3
        time.sleep(3)


def main(screen):
    global go
    curses.initscr()
    screen.clear()
    while go:
        screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
        c = screen.getch()
        if c == ord('q'):
            go = False


if __name__ == '__main__':
    t = Thread(target=update_ints)
    t.setDaemon(True)
    t.start()
    curses.wrapper(main)
预期的: 将显示x、y和z的值,并反映无需输入的增量

实际结果: x、y和z的值分别保持为1、2和3,并且仅在我按键时更新

-----------编辑: 这与预期的效果一样:

import curses
import time
from threading import Thread

x, y, z = 0, 0, 0
go = True
def update_ints():
    global x, y, z
    x += 1
    y += 2
    z += 3


def main(screen):
    global go
    curses.initscr()
    screen.clear()
    while go:
        update_ints()
        screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
        c = screen.getch()
        if c == ord('q'):
            go = False
        time.sleep(3)


if __name__ == '__main__':
    curses.wrapper(main)

但是我需要从线程更新值。

问题是
c=screen.getch()
阻塞了循环并阻止了值的更新

删除

c = screen.getch()
if c == ord('q'):
   go = False
。。。产生了预期的结果


感谢NEGR KITAEC

为什么
def increment_ints():
但是
t=Thread(target=update_ints)
?感谢您的快速响应!我正在模拟我的另一个脚本的基础结构-它更新线程中的值,我希望显示的文本反映更新的值。如果用输入和打印替换curses调用,它是否按预期工作?是的。这将逐行打印每个
x:{x},y={y},z={z}
,但所需的输入是一个静态屏幕,其中的值已更新到位