• <tfoot id='og5GA'></tfoot>

      <bdo id='og5GA'></bdo><ul id='og5GA'></ul>
      <legend id='og5GA'><style id='og5GA'><dir id='og5GA'><q id='og5GA'></q></dir></style></legend>
      1. <i id='og5GA'><tr id='og5GA'><dt id='og5GA'><q id='og5GA'><span id='og5GA'><b id='og5GA'><form id='og5GA'><ins id='og5GA'></ins><ul id='og5GA'></ul><sub id='og5GA'></sub></form><legend id='og5GA'></legend><bdo id='og5GA'><pre id='og5GA'><center id='og5GA'></center></pre></bdo></b><th id='og5GA'></th></span></q></dt></tr></i><div id='og5GA'><tfoot id='og5GA'></tfoot><dl id='og5GA'><fieldset id='og5GA'></fieldset></dl></div>
      2. <small id='og5GA'></small><noframes id='og5GA'>

        如何捕获 Python 解释器的输出并在 Text 小部件中显示?

        How to capture output of Python#39;s interpreter and show in a Text widget?(如何捕获 Python 解释器的输出并在 Text 小部件中显示?)
          <tbody id='XtHEN'></tbody>
          <i id='XtHEN'><tr id='XtHEN'><dt id='XtHEN'><q id='XtHEN'><span id='XtHEN'><b id='XtHEN'><form id='XtHEN'><ins id='XtHEN'></ins><ul id='XtHEN'></ul><sub id='XtHEN'></sub></form><legend id='XtHEN'></legend><bdo id='XtHEN'><pre id='XtHEN'><center id='XtHEN'></center></pre></bdo></b><th id='XtHEN'></th></span></q></dt></tr></i><div id='XtHEN'><tfoot id='XtHEN'></tfoot><dl id='XtHEN'><fieldset id='XtHEN'></fieldset></dl></div>
          <legend id='XtHEN'><style id='XtHEN'><dir id='XtHEN'><q id='XtHEN'></q></dir></style></legend>

          <tfoot id='XtHEN'></tfoot>
          • <bdo id='XtHEN'></bdo><ul id='XtHEN'></ul>
                • <small id='XtHEN'></small><noframes id='XtHEN'>

                  本文介绍了如何捕获 Python 解释器的输出并在 Text 小部件中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a program in Python with PyQt, designed to run on Windows. This program makes a lot of operations and prints a lot of info. But as I want to freeze it and don't want the prompt screen to appear, I want that all that info appears in the main application, in a QTextEdit or so. How can i make the program work so it gets the output from the interpreter and shows it on the textEdit at the same time, just like it does on the real interpreter?

                  解决方案

                  I assume that with "output from the interpreter", you mean output written to the console or terminal window, such as output produced with print().

                  All console output produced by Python gets written to the program's output streams sys.stdout (normal output) and sys.stderr (error output, such as exception tracebacks). These are file-like objects.

                  You can replace these streams with your own file-like object. All your custom implementation must provide is a write(text) function. By providing your own implementation, you can forward all output to your widget:

                  class MyStream(object):
                      def write(self, text):
                          # Add text to a QTextEdit...
                  
                  sys.stdout = MyStream()
                  sys.stderr = MyStream()
                  

                  If you ever need to reset these streams, they are still available as sys.__stdout__ and sys.__stderr__:

                  sys.stdout = sys.__stdout__
                  sys.stderr = sys.__stderr__
                  


                  Update

                  Here is some working code for PyQt4. First define a stream that reports data written to it with a Qt signal:

                  from PyQt4 import QtCore
                  
                  class EmittingStream(QtCore.QObject):
                  
                      textWritten = QtCore.pyqtSignal(str)
                  
                      def write(self, text):
                          self.textWritten.emit(str(text))
                  

                  Now, in your GUI, install an instance of this stream to sys.stdout and connect the textWritten signal to a slot that writes the text to a QTextEdit:

                  # Within your main window class...
                  
                  def __init__(self, parent=None, **kwargs):
                      # ...
                  
                      # Install the custom output stream
                      sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
                  
                  def __del__(self):
                      # Restore sys.stdout
                      sys.stdout = sys.__stdout__
                  
                  def normalOutputWritten(self, text):
                      """Append text to the QTextEdit."""
                      # Maybe QTextEdit.append() works as well, but this is how I do it:
                      cursor = self.textEdit.textCursor()
                      cursor.movePosition(QtGui.QTextCursor.End)
                      cursor.insertText(text)
                      self.textEdit.setTextCursor(cursor)
                      self.textEdit.ensureCursorVisible()
                  

                  这篇关于如何捕获 Python 解释器的输出并在 Text 小部件中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <tbody id='oKnfu'></tbody>
                    <bdo id='oKnfu'></bdo><ul id='oKnfu'></ul>

                    <small id='oKnfu'></small><noframes id='oKnfu'>

                        <tfoot id='oKnfu'></tfoot>

                          • <i id='oKnfu'><tr id='oKnfu'><dt id='oKnfu'><q id='oKnfu'><span id='oKnfu'><b id='oKnfu'><form id='oKnfu'><ins id='oKnfu'></ins><ul id='oKnfu'></ul><sub id='oKnfu'></sub></form><legend id='oKnfu'></legend><bdo id='oKnfu'><pre id='oKnfu'><center id='oKnfu'></center></pre></bdo></b><th id='oKnfu'></th></span></q></dt></tr></i><div id='oKnfu'><tfoot id='oKnfu'></tfoot><dl id='oKnfu'><fieldset id='oKnfu'></fieldset></dl></div>
                            <legend id='oKnfu'><style id='oKnfu'><dir id='oKnfu'><q id='oKnfu'></q></dir></style></legend>