你有没有面对文本框没有回应?

2024-05-16 04:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要知道你是否在使用文本框显示大量数据时遇到过减速甚至挂起的情况。你知道吗

在我的例子中,我使用subprocess运行一个模拟脚本,它的stdout显示在文本框中。 文本可以是MBs。你知道吗

我尝试了两种实现:

1)文本框等待模拟(子进程)完成,然后才能显示标准输出数据。结果很好。 但唯一的问题是我想实时显示stdout数据。你知道吗

2)我开始在这里实时显示数据文本框很容易显示小进程的标准数据。 但是,当我运行长时间的模拟脚本时,它们被夹在中间。 我知道由于没有生成相关的输出文件,模拟脚本的执行被暂停。 屏幕悬挂与我们在windows中遇到的类似。你知道吗

关于模拟脚本:这是一个脚本,可以使用其他脚本运行许多其他子进程(一次一个子进程)。你知道吗

如果你有什么解决办法,请指教?我可以用画布代替文本框吗?有帮助吗?你知道吗

运行函数如下:

def run():

    filename = str(run_file_name.get())
    command.set("Running "+filename)

    #Creating new Window to display output 
    t = Toplevel(root)
    t.title('output Run Display')
    t.geometry('800x1000-5+40')
    t.state('normal')
    little = Label(t, text="OUTPUT LOG").grid(column = 0, row = 0)
    log = Text(t, state='disabled', width=115, height=150, wrap='none')
    log.grid(row = 1, column = 0)

    test=subprocess.Popen(filename,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)# stderr=subprocess.PIPE)
    #stdout
    while True:
        line_out = test.stdout.readline()
            line_er  = test.stderr.readline()
        if line_out == "" and line_er == "":
            break
            else:
                log['state'] = 'normal'
                log.insert('end', line_out)
                    log.insert('end', line_er)
                log['state'] = 'disabled'
                    print line_out
                    print line_er
                    t.update()

主要目标是运行子进程并在textbox上显示所有输出,从而将用户与命令提示符界面断开链接。 我调试尝试的备注: 当我用一个打印数字1到5000的示例脚本(sample\u script)运行同一个gui脚本时,它运行得很好。这意味着脚本的流程是正常的。 但测试环境和我的实际脚本运行环境之间有一个主要区别:那就是我的子进程运行脚本(说:-脚本1)实际上是一个运行其他脚本的父csh脚本(说:-脚本1.1在它下面写1.2)。 因此,请注意以下几点:

The execution halts when script1 has issued commands to launch script1.1 The sample_script(which prints number) is working fine though.

调试更新: 我将以下脚本作为示例脚本运行,并注意到以下结果:

set i = 450
while ($i > 1)
  echo i is $i
  set i = `expr "$i" - 1`
end 

By running the above example, i found that the output appears only after subprocess is complete. However, In my case if i put i = 550, the terminal stays in wait state and output doesn't appear(even after many minutes). Hence the process stuck. The i = 550 number may be higher or lower for you. When i am running i = 550, using print statements i found that the execution of sample_script stops at i = 98. I am unable to figure out why!!

如果您需要更多信息,请及时通知我。


Tags: the数据脚本logoutput进程stdoutline
2条回答

我已经使用tk将近20年了,还没有注意到大量文本的任何问题,不过我想我从来没有尝试过加载超过10万行的内容。你想显示多少数据?你是使用大量的标签和不同的字体,还是这只是纯文本?你知道吗

当然,如果您正在生成子进程并等待它完成,那么整个GUI将变得没有响应。Tk是单线程的,因此当您等待进程完成时,事件循环无法运行,因此您的程序将冻结。您需要确保用于运行流程和管理其输出的任何方案都不会阻塞事件循环。你知道吗

我的猜测是,这不是文本小部件的限制,切换到画布也没有帮助。问题可能出在您的代码中,但是如果没有看到您的代码,就不可能想出解决方案。你知道吗

作为一种解决方法,也许不是实时显示整个输出,而是实时显示最后100行?完整的输出仍然可以放入日志文件中,以便在以后需要时加载。你知道吗

相关问题 更多 >