从Spyder的QTDialog窗口执行时print()的行为不正确 [英] Incorrect behaviour of print() when executed from within a QTDialog window in Spyder

查看:222
本文介绍了从Spyder的QTDialog窗口执行时print()的行为不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个非常简单的界面来探索/绘制csv文件。我的目标是最终探索,而不是构建软件,因为我不是开发人员,更像是一个绝望的用户: - )

I am working on a very simple interface to explore/graph csv files. My aim is ultimately to explore, not to build software as I am not a developer, more of a "desperate user" :-)

我正在利用找到的代码< a href =https://stackoverflow.com/questions/12459811/how-to-embed-matplotib-in-pyqt-for-dummies>在此示例中

I am leveraging the code found in this example

这是我在Python和GUI中的第一步,所以我倾向于在我的调用中放置打印消息,以便我可以或多或少地跟踪发生的事情。如果我在Spyder中运行代码,这就是我发现奇怪行为的地方。

These are my first steps both in Python and in GUI, so I tend to put print messages in my calls so that I can more or less track what is happening. And this is where I found a strange behavior if I run the code from within Spyder.

import sys
import os
from PyQt4 import QtGui
import pandas as pd
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

# QtGui.QDialog

class Window(QtGui.QDialog):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Just some extra button to mess around
        self.button= QtGui.QPushButton('Push Me')
        self.button.clicked.connect(self.do_print)

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)


    def do_print(self):
        print('Hello World!!')


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

奇怪的是,如果我按下按钮,Ipython控制台上什么也没发生。我第二次推,然后两个Hello World!打印出现。

The strange behavior is that if I push the button once, nothing happens on the Ipython console. By the second time I push, then two "Hello World!" printouts appear.

另一方面,如果我只是从Windows Shell中启动我的脚本:

If, on the other hand, I just launch my script from within a Windows Shell:

python my_simple_test.py

然后一切都按预期工作。

Then everything works as expected.

我在Spyder内做错了什么?

What am I then doing wrong from within Spyder?

谢谢,
Michele

Thanks, Michele

推荐答案

IPython缓冲stdout与终端有点不同。当打印某些内容时,它会查看自上次刷新缓冲区以来已经过了多长时间,如果它超过某个阈值,则会再次刷新它。因此,第二次单击按钮时,它会刷新标准输出,并且您会看到两个输出。

IPython buffers stdout a bit differently from a terminal. When something is printed, it looks at how long it has been since it last flushed the buffer, and if it's longer than some threshold, it flushes it again. So the second time you click the button, it flushes stdout, and you see both outputs.

您可以强制它立即刷新,如下所示:

You can force it to flush immediately like this:

print('Hello World!!')
sys.stdout.flush()

这篇关于从Spyder的QTDialog窗口执行时print()的行为不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆