PyQt5 insertPlainText from Another Class Python 3.8 [duplicate] - python

This question already has answers here:
Variable scopes in Python classes
(4 answers)
Closed 2 years ago.
Clicking button "OK" on "otherWindow" should cause the MainWindow's QTextEdit to insert text "WORKS!".
Problem is, it does execute print("Print Works"), but insertPlainText seems to do nothing when called from another function.
The def printText(self, message): function itself isn't broken, it works as it's supposed to, as you can verify by clicking on the "Message" button on Main Window.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QPushButton
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.myLayout = QVBoxLayout()
self.status = QTextEdit()
self.status.setStyleSheet("QTextEdit {min-width:500px;min-height:200px;}")
self.status.insertPlainText("test")
self.btnYes = QPushButton("other window")
self.btnPrint = QPushButton("Message")
self.btnYes.clicked.connect(self.showOtherWindow)
self.btnPrint.clicked.connect(self.btnPrintClick)
self.myLayout.addWidget(self.btnPrint)
self.myLayout.addWidget(self.btnYes)
self.myLayout.addWidget(self.status)
self.setLayout(self.myLayout)
def setMainText(self, message):
self.status.insertPlainText("test")
def showOtherWindow(self):
self.otherWindow = otherWindow()
self.otherWindow.show()
def btnPrintClick(self):
self.printText("button clicked")
def printText(self, message):
self.status.insertPlainText("\n" + message)
print("Print Works")
class otherWindow(QWidget):
def __init__(self):
super(otherWindow, self).__init__()
self.button = QPushButton("OK")
self.layout2 = QVBoxLayout()
self.button.clicked.connect(self.btnClick)
self.layout2.addWidget(self.button)
self.setLayout(self.layout2)
self.setFixedSize(200,150)
def btnClick(self):
MainWindow().printText("WORKS!")
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

It doesn't work because you're setting the text on a new window (which is closed immediately).
def btnClick(self):
MainWindow().printText("WORKS!")
When you call MainWindow(), you're actually creating a NEW instance of MainWindow, and the text is actually updated for that window, but you can't see it as it is immediately garbage collected and deleted right after the function returns.
You need to access the existing instance, or find a way to communicate with it (usually using signals).
In the following example, I'm adding a reference to the main window to the OtherWindow constructor, and then access it's method afterwards:
class MainWindow(QWidget):
# ...
def showOtherWindow(self):
self.otherWindow = OtherWindow(self)
self.otherWindow.show()
class OtherWindow(QWidget):
def __init__(self, mainWindow=None):
super(OtherWindow, self).__init__()
self.mainWindow = mainWindow
# ...
def btnClick(self):
if self.mainWindow:
self.mainWindow.printText("WORKS!")
self.close()
Note: I capitalized the OtherWindow class name, lower case names should be only used for variables and attributes.

Related

QtCore.signal not doing anything

I am a beginner with PyQt5 and I am having trouble to use QtCore.signal
I'd like to send a signal when I press my buttons and switch the current widget displayed.
I don't have any errors when I run my code but when I press the buttons nothing happen and I guess it is because I am doing something wrong with the QtCore.Signal
Here is my code :
from PySide2 import QtCore, QtWidgets
from ui_Page_accueil import Ui_MainWindow
from ui_NouvelleVerif import Ui_Dialog as Ui_NouvelleVerif
from ui_NouvelleVerifEssieux import Ui_Dialog as Ui_NouvelleVerifEssieux
import sys
class MainWindowUi(Ui_MainWindow):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
#self.pushButton.clicked.connect(self.pushbutton_handler1)
self.pushButton_2.clicked.connect(self.pushbutton_handler2)
#def pushbutton_handler1(self):
# self.to_MainWindow.emit()
def pushbutton_handler2(self):
self.to_NouvelleVerif.emit()
class NouvelleVerifUi(QtWidgets.QWidget, Ui_NouvelleVerif):
to_MainWindow = QtCore.Signal()
to_NouvelleVerifEssieux = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
#self.pushButton_2.clicked.connect(self.pushbutton_handler2)
self.pushButton_3.clicked.connect(self.pushbutton_handler3)
def pushbutton_handler1(self):
self.to_MainWindow.emit()
#def pushbutton_handler2(self):
# self.switch_window.emit()
def pushbutton_handler3(self):
self.to_NouvelleVerifEssieux.emit()
class NouvelleVerifEssieuxUi(QtWidgets.QWidget, Ui_NouvelleVerifEssieux):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
def pushbutton_handler1(self):
self.to_NouvelleVerif.emit()
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
def show_MainWindow(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.MainWindow = MainWindowUi()
self.NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
def main():
controller = Controller()
controller.widget.show()
sys.exit(controller.app.exec_())
if __name__ == '__main__':
main()
#musicamante I don't know why I though the QtCore.Signal could be call even if he was in a function which has not been called. I did what you said and I realise I made another mistake with the widgets.
In the controller class they are create and add to the QStackedWidget in the __init__, but I was creating new ones and trying to set them as CurrentWidget without adding them to the QStackedWidget.
#alexpdev I wanted to navigate through my three differents with pushButton UI this way :
Start with MainWindowUI
MainWindowUI pushButton_2.clicked --> set the current widget display to NouvelleVerifUI
NouvelleVerifUI pushButton_1.clicked --> set the current widget display back to MainWindowUI
NouvelleVerifUI pushButton_3.clicked --> set the current widget display to NouvelleVerifEssieuxUI
NouvelleVerifEssieuxUI pushButton_1.clicked --> set the current widget display back to NouvelleVerifUI
Now everything work I did what you said #musicamante and I also use the UI created at first.
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
def show_MainWindow(self):
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
Thank you all for your time

How do I direct console output to a pyqt5 plainTextEdit widget with Python?

I am trying to display console output of a python script in a QplainTextEdit widget in PyQt5.
I am getting this error:
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
I have defined my objects in the pyqt GUI file and I believe that I have all the imports.
Update
I have amended the code in this question:
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QFileDialog, QPlainTextEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image, ImageQt, ImageEnhance
# from PyQt5.QtGui import Qt
from pyqtgraph.examples.text import text
from covid19gui_V3 import Ui_MainWindow
import os
import sys
input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
text_edit = QPlainTextEdit()
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
textWritten = QtCore.pyqtSignal(str)
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.ShowIButton.clicked.connect(self.do_test)
self.chooseStudy.clicked.connect(self.do_choosestudy)
self.RunButton_3.clicked.connect(self.do_runstudy)
self.scene = QtWidgets.QGraphicsScene(self)
self.graphicsView.setScene(self.scene)
w, h = input_img.size
self.pixmap_item = self.scene.addPixmap(QtGui.QPixmap())
# self.graphicsView.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
self.graphicsView.update()
self.plainTextEdit.update()
self.level = 1
self.enhancer = None
self.timer = QtCore.QTimer(interval=500, timeout=self.on_timeout)
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def write(self, text):
self.textWritten.emit(str(text))
#QtCore.pyqtSlot()
def do_test(self):
# input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
self.enhancer = ImageEnhance.Brightness(input_img)
self.timer.start()
self.ShowIButton.setDisabled(True)
#QtCore.pyqtSlot()
def on_timeout(self):
if self.enhancer is not None:
result_img = self.enhancer.enhance(self.level)
qimage = ImageQt.ImageQt(result_img)
self.pixmap_item.setPixmap(QtGui.QPixmap.fromImage(qimage))
if self.level > 7:
self.timer.stop()
self.enhancer = None
self.level = 0
self.ShowIButton.setDisabled(False)
self.level = 1
self.ShowIButton.setDisabled(False)
#QtCore.pyqtSlot()
def do_choosestudy(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
#QtCore.pyqtSlot()
def do_runstudy(self):
os.system("df -h")
# filetext = open('screenout.txt').read()
# filetext.close()
# textViewValue = self.plainTextEdit.toPlainText()
# QPlainTextEdit.appendPlainText(self, str(textViewValue))
# sys.stdout = self(textWritten=self.textWritten)
self.normalOutputWritten(text_edit)
def __del__(self):
# Restore sys.stdout
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text_edit):
#cursor = self.plainTextEdit.textCursor()
#cursor.movePosition(QtGui.QTextCursor.End)
#cursor.insertText(text_edit)
self.plainTextEdit.appendPlainText(text_edit)
#self.plainTextEdit.ensureCursorVisible()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
How can I make this work correctly?
Update 2
I indeed DID do research into the topic and this is one of the main resources I used to try to solve the issue before I posted my question: How to capture output of Python's interpreter and show in a Text widget?
Update 3
I have revised my code in the post to reflect code suggestions in the link I used to help me with my issue.
I am still unable to get this to run correctly. I now get this error:
self.plainTextEdit.appendPlainText(text_edit) TypeError:
appendPlainText(self, str): argument 1 has unexpected type
'QPlainTextEdit'
I have a user interface, TableManagerWindow, that I've been maintaining and developing in Qt designer. After converting via pyuic to a *.py file, I was able to implement what Ferdinand Beyer had suggested in the link you provided above. Simple button to print text to terminal and it indeed does get appended to the QTextEdit widget via append(). Not sure this fits the bill for you for some reason, but I can vouch that it worked for me as well. I'm not savvy enough to get the nuance that is causing your issue, but figured I'd put this here just in case. Admins feel free to delete this if it's extraneous, but it works.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# Define a stream, custom class, that reports data written to it, with a Qt signal
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class Ui_TableManagerWindow(object):
def setupUi(self, TableManagerWindow):
#define all of my widgets, layout, etc here
.
.
.
# Install a custom output stream by connecting sys.stdout to instance of EmmittingStream.
sys.stdout = EmittingStream(textWritten=self.output_terminal_written)
# Create my signal/connections for custom method
self.source_dir_button.clicked.connect(self.sourceDirButtonClicked)
self.retranslateUi(TableManagerWindow)
QtCore.QMetaObject.connectSlotsByName(TableManagerWindow)
def retranslateUi(self, TableManagerWindow):
.
.
.
#custom method that prints to output terminal. The point is to have this emmitted out to my QTextEdit widget.
def sourceDirButtonClicked(self):
for i in range(10):
print("The Source DIR button has been clicked " + str(i) + " times")
#custom method to write anything printed out to console/terminal to my QTextEdit widget via append function.
def output_terminal_written(self, text):
self.output_terminal_textEdit.append(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
TableManagerWindow = QtWidgets.QMainWindow()
ui = Ui_TableManagerWindow()
ui.setupUi(TableManagerWindow)
TableManagerWindow.show()
sys.exit(app.exec_())

revert rejected dialogue/form to show last accepted values on reopen

I'm setting up an "options" dialog in a program, where I can change some values and close the dialog with Ok/Cancel to accept of reject my changes. After closing the dialog with cancel and reopening it, i would like the last accepted values to be displayed, however I am know sure how to implement this.
Below is a very simplified version of my code. I chose to instanciate the dialog only once (as opposed to creating a new instance each time I call the dialog), mainly to avoid having to call the __init__ and import data from save files each time I open the dialog.
from PyQt5.QtWidgets import QMainWindow, QPushButton,\
QApplication, QTextEdit, QDialog, QDialogButtonBox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
btn = QPushButton('open text 1', self)
btn.move(10, 10)
btn.clicked.connect(self.open_dlg)
self.txtdlg = TextDialog()
def open_dlg(self):
if self.txtdlg.exec_() == QDialog.Accepted:
print(self.txtdlg.preferences)
class TextDialog(QDialog):
def __init__(self):
super().__init__()
self.preferences = "text here"
self.resize(200, 150)
self.textedit = QTextEdit(self)
self.textedit.resize(200, 100)
self.textedit.setText(self.preferences)
btns = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
btns.move(20, 100)
btns.accepted.connect(self.save_and_close)
btns.rejected.connect(self.reject)
def save_and_close(self):
self.preferences = self.textedit.toPlainText()
self.accept()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(app.exec_())
As it is, after clicking Cancel the dialog keeps any unsaved changes to its widgets if I reopen it. My fist idea was to connect the cancel button to a close_without_saving method that updates the dialog to the last saved values before closing, but the displayed values will not be up to date if preferences is changed for some reason while the dialog is invisible. Can I run some code when i call exec_ ? Or is the logic behind my implementation wrong somehow?
You have to implement a method that sets the values of the dialog to the default values:
# ...
class MainWindow(QMainWindow):
# ...
def open_dlg(self):
self.txtdlg.reset()
if self.txtdlg.exec_() == QDialog.Accepted:
print(self.txtdlg.preferences)
class TextDialog(QDialog):
# ...
def reset(self):
self.preferences = "text here"
self.textedit.setText(self.preferences)
def save_and_close(self):
self.preferences = self.textedit.toPlainText()
self.accept()
# ...

PyQt5: Creation of Windows with Classes [duplicate]

This question already has answers here:
Understanding Python super() with __init__() methods [duplicate]
(7 answers)
Closed 4 years ago.
I have a PyQt5 application built that looks like so (I know I have a lot of imports, I am learning so I want complete freedom at the moment):
import sys
from PyQt5.QtGui import *
from PyQt5.QWidgets import *
from PyQt5.QtCore import *
class Menu(QMainWindow):
def __init__(self)
super().__init__()
#create bar
bar = self.menuBar()
#create bar menus
file = bar.addMenu("File")
about = bar.addMenu("About")
#create actions
quit_action = QAction("&Quit", self)
quit_action.setShortcut('Ctrl+Q')
about_action = QAction("&About...", self)
#add actions
file.addAction(quit_action)
about.addAction(about_action)
#what to do with actions
quit_action.triggered.connect(self.quit_func)
about_action.triggered.connect(self.about_func)
#window properties
self.setWindowTitle("Hello World")
self.resize(600, 400)
self.show()
def quit_func(self):
sys.exit()
def about_func(self):
pass
class About(QWidget):
def __init__(self):
super().__init__(parent)
#widgets
self.l1 = QLabel('Hello World')
self.l1.setAlignment(Qt.AlignCenter)
self.l2 = QLabel('Description of the Application')
self.l2.setAlignment(Qt.AlignCenter)
#horiz box
h_box = QHBoxLayout()
h_box.addStretch()
h_box.addWidget(self.l2)
h_box.addStretch()
#vert box
v_box = QVBoxLayout()
v_box.addWidget(self.l1)
v_box.addLayout(h_box)
v_box.addStretch()
self.setLayout(v_box)
#window properties
self.setWindowTitle("About Hello World")
self.setFixedSize(250,150)
self.show()
if not QApplication.instance()
app = QApplication(sys.argv)
else:
app = QApplication.instance()
main = Menu()
main.show()
sys.exit(app.exec())
I want the about_func() function to call to the About() class, so I can open a window separate from my Main Window created by Menu() class.
This code is throwing the error:
TypeError: QMainWindow(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'sip.wrappertype'
in reference to the super().__init__() in line 9.
How could I implement this in a working fashion? Feel free to criticize any aspect of my code.
(Edited to clarify question)
From your code it's not very clear if you are using Python 2 or 3, anyway, the basic syntax of super is:
super(yourClass, instance).method(args)
So, in your case they are both wrong :-) The first one should be:
class Menu(QMainWindow):
def __init__(self, parent=None):
super(Menu, self).__init__(parent)
Also, from Python3 the arguments of super() can be omitted, so the second example could be:
class About(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
Read carefully the Built-in Functions. I know it's a long page, but it contains some of the fundamentals of Python, and studying/understanding them is almost mandatory.

I want to know how to use pyqt5 to load the created widget at once

Sorry. I will modify the contents. I would like to load a widget inside def test by pressing Qbutton. Can not you use QStackedWidget to load the widget's configured functions? I've compiled the class and called it, but only a = QLineEdit ('Qline', self). I wonder what should be done to switch widgets.
You can also create a table like html using pyqt5.
import sys
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.stacked = QStackedWidget(self)
self.FirstpUI()
def FirstpUI(self):
self.btn1 = QPushButton('test1', self)
self.btn1.move(50,50)
self.btn1.clicked.connect(self.btn1_click)
def test(self):
a = QLineEdit('Qline', self)
b = QLineEdit('Qline2', self)
c = QPushButton('button', self)
a.move(0, 0)
b.move(100, 0)
c.move(50,50)
c.clicked.connect(self.btn2_click)
def btn1_click(self):
self.btn1.deleteLater()
self.stacked.addWidget(self.test())
self.stacked.setCurrentIndex(self.stacked.currentIndex()+1)
def btn2_click(self):
QMessageBox.about(self,'hello','hello2')
if __name__ == "__main__":
app = QApplication(sys.argv)
fream = MainWindow()
fream.show()
app.exec_()
May be I don't know what you real want,because I know that little, I think You can use QtDesigner,it's very useful

Categories