Cannot import PyQt4.QtGui

Add the the PyQt4 directory containing Qt’s applications and DLLs to your PATH environment variable. In PowerShell, provided you didn’t change any of your install paths, that’d be $env:path += ‘;C:\Python26\Lib\site-packages\PyQt4\bin’

Hiding console window of Python GUI app with py2exe

Yep, it is possible. If I use setup(console=[‘__main__.py’], options={“py2exe”:{“includes”:[“sip”]}}) It creates a console app, however if I use setup(windows=[‘__main__.py’], options={“py2exe”:{“includes”:[“sip”]}}) it does not show console on .exe file. But output is dumped on main.exe.log file in the .exe folder. Be careful.

How to align the text to center of cells in a QTableWidget

This line of code: item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter) will not work properly, because it throws away the item it creates before assigning it to the variable. The variable will in fact be set to None, which is the return value of setTextAlignment(). Instead, you must do this: item = QTableWidgetItem(scraped_age) # create the item item.setTextAlignment(Qt.AlignHCenter) # … Read more

Remove all items from a layout

The safest way to clear a layout is to extract the items with its takeAt method, and then explicitly delete any widgets with deleteLater: def clearLayout(self, layout): if layout is not None: while layout.count(): item = layout.takeAt(0) widget = item.widget() if widget is not None: widget.deleteLater() else: self.clearLayout(item.layout())

Print out Python console output to Qtextedit

The output consoles produced by Python are written to the program output streams sys.stdout (normal output) and sys.stderr (error output, such as exception tracebacks). In this case we will use stdout class Stream(QtCore.QObject): newText = QtCore.pyqtSignal(str) def write(self, text): self.newText.emit(str(text)) class Window(QtGui.QMainWindow): def __init__(self): super(Window, self).__init__() self.setGeometry(50, 50, 500, 300) self.setWindowTitle(“PyQT tuts!”) self.setWindowIcon(QtGui.QIcon(‘pythonlogo.png’)) self.home() sys.stdout … Read more

PySide / PyQt detect if user trying to close window

Override the closeEvent method of QWidget in your main window. For example: class MainWindow(QWidget): # or QMainWindow … def closeEvent(self, event): # do stuff if can_exit: event.accept() # let the window close else: event.ignore() Another possibility is to use the QApplication‘s aboutToQuit signal like this: app = QApplication(sys.argv) app.aboutToQuit.connect(myExitHandler) # myExitHandler is a callable