Return value from button click

Store the file_path in a class level variable and update that value in your button click method. self.file_path = None self.Button_open.clicked.connect(self.OpenTextFile) And then, def OpenTextFile(self): dialog = QtGui.QFileDialog() dialog.setWindowTitle(“Choose a file to open”) dialog.setFileMode(QtGui.QFileDialog.ExistingFile) dialog.setNameFilter(“Text (*.txt);; All files (*.*)”) dialog.setViewMode(QtGui.QFileDialog.Detail) filename = QtCore.QStringList() if(dialog.exec_()): file_name = dialog.selectedFiles() plain_text = open(file_name[0]).read() self.Editor.setPlainText(plain_text) self.file_path = str(file_name[0]) Also … Read more

How to capture output of Python’s interpreter and show in a Text widget?

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 … Read more

QLabel is not updated unless the mainWindow is unfocused

the issue is present in PyQt5 since 5.11.0 (tested 5.11.x, 5.12.x and 5.13) and PySide2 v.5.13 on MacOS (tested 10.14 and 10.12.6). The v.5.10.1 works fine. The issue does not exist under Linux and Windows Adding a call to the repaint method fix the issue. def setTextHelloWorld(self): self.label.setText(“Hello World”) self.label.repaint()

How to check MouseButtonPress event in PyQt6?

One of the main changes that PyQt6 enums use python enums so you must use the enumeration name as an intermediary, in your case MouseButtonPress belongs to the Type enum and RightButton to MouseButtons so you must change it to: def eventFilter(self, QObject, event): if event.type() == QEvent.Type.MouseButtonPress: if event.button() == Qt.MouseButtons.RightButton: print(“Right button clicked”) … Read more

PyQt GUI size on high resolution screens

Before app is loaded, and not while loading. insert: if hasattr(QtCore.Qt, ‘AA_EnableHighDpiScaling’): PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) if hasattr(QtCore.Qt, ‘AA_UseHighDpiPixmaps’): PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) like so : import PyQt5 from PyQt5 import QtCore, QtGui, uic, QtWidgets from PyQt5.QtWidgets import QWidget if hasattr(QtCore.Qt, ‘AA_EnableHighDpiScaling’): PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) if hasattr(QtCore.Qt, ‘AA_UseHighDpiPixmaps’): PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) class MyWindow(PyQt5.QtWidgets.QMainWindow): def __init__(self): super(MyWindow, self).__init__() And if that still … Read more

Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread

Firstly, +1 for realising how thread-unsafe many of the examples on stack overflow are! The solution is to use a thread-safe object (like a Python Queue.Queue) to mediate the transfer of information. I’ve attached some sample code below which redirects stdout to a Python Queue. This Queue is read by a QThread, which emits the … Read more