【问题标题】:Python 2.7 to 3.7 tkinter scrolledtext is not behavingPython 2.7 到 3.7 tkinter scrolledtext 没有表现
【发布时间】:2020-10-30 17:50:39
【问题描述】:

我有一个使用 Tkinter 并将其移植到 3.7 的 Python 2.7 GUI 它将子进程标准输出通过管道传输到滚动文本小部件,并在 2.7 中正常工作,但在 3.7 中,它不断更新小部件,我无法滚动。代码sn-p:

#lauch main
    proc = subprocess.Popen(["python", testPath, filePath] + script_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0)

    #clear the text box widget
    text.delete("1.0", END)

    #check for alternate main.py
    if (alt_main_flag == True):
        text.insert(END, "Using alternate MAIN script in " + filePath + '\n')

    #send the scripts output to the GUI text box
    # for 3.x, had to use bytes() conversion prior to 'in'
    for line in iter(proc.stdout.readline,''):
        if (bytes("Starting ", 'ascii') in line):
            line = line.strip()
            statusvar.set(line)
        else:
            text.insert(END, line)
        if (bytes("end of program", 'ascii') in line):
            statusvar.set("Ready")
        #update in "real time"
        text.see(END)
        root.update()
        text.update_idletasks()

建议?

标记

【问题讨论】:

  • 当我删除 text.see(END) 行时,情况会“更好”......当子进程完成时我可以滚动......但当然文本不会更新/滚动'实时'......它保持在顶部。一定是更好的方法!
  • 在我杀死 GUI 时注意到,python 吐出这个错误:文件 "Z:\Projects\Python_Test_Scripts\Gui_Test\3_8_GUI\GUI_2.py",第 675 行,在 startMain text.update_idletasks() 文件中" C:\Python37\lib\tkinter_init_.py",第 1182 行,在 update_idletasks self.tk.call('update', 'idletasks') _tkinter.TclError: can't invoke "update"命令:应用程序已被破坏 - 就像 for 循环仍在运行!为什么???这可以解释我所看到的一切。
  • 这就是问题所在! for 循环永远不会退出。什么鬼?

标签: python tkinter python-2to3


【解决方案1】:

找到了解决方法:在 for 循环中添加了一个中断。

for line in iter(proc.stdout.readline,''):
        if (bytes("Starting ", 'ascii') in line):
            line = line.strip()
            statusvar.set(line)
        else:
            text.insert(END, line)
        if (bytes("end of program", 'ascii') in line):
            statusvar.set("Ready")
        if (len(line) == 0):
            break
            
        #update in "real time"
        text.see(END)
        root.update()
        text.update_idletasks()

【讨论】:

  • 您可以在subprocess.Popen(...)中设置text=True,则不需要使用bytes(...)。您也可以简单地使用for line in proc.stdout:。而且你不需要打电话给root.update()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多