Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 GUI在cmd运行时冻结,但不是通过Pycharm_Python_Python 3.x_Tkinter_Pexpect - Fatal编程技术网

Python GUI在cmd运行时冻结,但不是通过Pycharm

Python GUI在cmd运行时冻结,但不是通过Pycharm,python,python-3.x,tkinter,pexpect,Python,Python 3.x,Tkinter,Pexpect,所以我已经看了很长一段时间了,但我不能解决这个问题 这里的目标是让它逐行显示从cmd/terminal到tkinter ScrolledText/Text小部件的实时结果,除非在Windows上通过cmd运行 我正在Linux(Kali)和Windows10上运行代码。如果我在Windows10上使用Pycharm运行它,那么它将平稳运行,GUI不会冻结,等待使用Pexpect运行代码。 如果我通过cmd(python tmp_stack.py)运行代码,那么它将冻结GUI,直到Pexpect完

所以我已经看了很长一段时间了,但我不能解决这个问题

这里的目标是让它逐行显示从cmd/terminal到tkinter ScrolledText/Text小部件的实时结果,除非在Windows上通过cmd运行

我正在Linux(Kali)和Windows10上运行代码。如果我在Windows10上使用Pycharm运行它,那么它将平稳运行,GUI不会冻结,等待使用Pexpect运行代码。 如果我通过cmd(
python tmp_stack.py
)运行代码,那么它将冻结GUI,直到Pexpect完成我要求它运行的文件。
在Linux终端上,代码运行良好,不会冻结(在针对Linux进行调整后)。
我将多个文件合并到
tmp_stack.py
中,以防止无真正原因地创建更多文件。
我已经测试过该配置在Windows和Linux上运行相同

旁注:我不介意更改为
subprocess.Popen
,如果它不会抱怨主循环并且可以工作,我也不介意使用
线程

requirements.txt-
pexpect==4.6.0

op.py:

import time

print('This is op.py')
for i in range(10):
    time.sleep(1)
    print(i)
oscheck.py:

import platform

MYPLATFORM = platform.system()
tmp_stack.py:

from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText

import oscheck

if oscheck.MYPLATFORM == 'Windows':
    from pexpect import popen_spawn
elif oscheck.MYPLATFORM == 'Linux':
    from pexpect import spawn


class TextFrame(Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.textarea = ScrolledText(master=self, wrap=WORD, bg='black', fg='white')
        self.textarea.pack(side=TOP, fill=BOTH, expand=True)

    def insert(self, text):
        self.textarea['state'] = 'normal'
        # Insert at the end of the TextArea.
        self.textarea.insert(END, text)
        self.textarea['state'] = 'disabled'
        self.update()


class TheFrame(TextFrame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.btn = Button(master=self, text="Run op", command=self.run_op)
        self.btn.pack(fill=X)
        # Ignore first child of pexpect on Linux because it is the bin bash prefix.
        self.linux_flag = True

    def run_op(self):
        filename = 'op.py'
        cmd = ['python', filename]

        self.cmdstdout = RunCommand(cmd)

        # Get root.
        root_name = self._nametowidget(self.winfo_parent()).winfo_parent()
        root = self._nametowidget(root_name)

        root.after(0, self.updateLines())

    def updateLines(self):
        for line in self.cmdstdout.get_child():
            if oscheck.MYPLATFORM == 'Linux' and self.linux_flag:
                self.linux_flag = False
                continue
            try:
                self.insert(line.decode('utf-8'))
            except TclError as e:
                print("Window has been closed.\n", e)
                self.close()
                break

    def close(self):
        self.cmdstdout.close()


class RunCommand:
    def __init__(self, command):
        command = ' '.join(command)
        if oscheck.MYPLATFORM == 'Windows':
            print('You are on Windows.')
            self.child = popen_spawn.PopenSpawn(command, timeout=None)
        elif oscheck.MYPLATFORM == 'Linux':
            print('You are on Linux.')
            self.child = spawn("/bin/bash", timeout=None)
            self.child.sendline(command)
        else:
            print('Not Linux or Windows, probably Mac')
            self.child = spawn(command, timeout=None)

    def get_child(self):
        return self.child

    def close(self):
        if oscheck.MYPLATFORM == 'Linux':
            self.child.terminate(True)


def on_close(root):
    root.quit()
    root.destroy()


root = Tk()

if oscheck.MYPLATFORM == 'Windows':
    root.state('zoomed')
elif oscheck.MYPLATFORM == 'Linux':
    root.attributes('-zoomed', True)

the_frame = TheFrame(root, padding="1")
the_frame.grid(column=0, row=0, sticky=N+W+S+E)

root.protocol("WM_DELETE_WINDOW", lambda: on_close(root))
mainloop()

尝试在禁用输出缓冲的情况下运行子Python进程

换言之,尝试更换线路

        cmd = ['python', filename]

另见

        cmd = ['python', '-u', filename]