[Python] Anti-Patterns in Python Programming

Anti-Patterns in Python Programming

Annunci

[Python] Redirect output to a QTextEdit

First, define a QTextEdit named QTextEdit_Global.
Then define the following:

class MyStream(QtCore.QObject):    
    def __init__(self,edit,out=None):
        self.edit=edit
        self.out=out
    def write(self,text):
        if len(text) > 1:
            self.edit.append(text.replace('\n',''))
            QtCore.QCoreApplication.processEvents() 

Finally

sys.stdout = MyStream(ui.QTextEdit_Global,sys.stdout) 

Source:
http://stackoverflow.com/questions/8356336/how-to-capture-output-of-pythons-interpreter-and-show-in-a-text-widget

[Python] [PyQt] Adding a toolbar to a MatplotlibWidget

First of all place your widget (MatplotlibWidget) in a layout (Layout) in a Form or MainWindow (MainForm), via Qt Designer if you like.

from PyQt4 import QtCore,QtGui 
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar

navi_toolbar = NavigationToolbar(MatplotlibWidget, MainForm)
Layout.addWidget(navi_toolbar)

Source:
http://stackoverflow.com/questions/10342078/adding-a-toolbar-to-a-mpl-figure-in-pyqt4