SEGV_MAPERR for PyQt5 object - python

Why does the following demo code produce a SEGV_MAPERR? How would one fix it?
Hint: Once one deletes the line annotated with "# Ref1", no error is produced. This is not easily done in the production code the problem is abstracted from.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebChannel
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setHtml("")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
page = self.webView.page() # REF1
for i in range(2):
self.init_page()
def init_page(self):
class EditObject(QtCore.QObject):
#QtCore.pyqtSlot(str)
def edit(self, s):
print("test")
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
print(1)
if poChannel is None:
poChannel = QtWebChannel.QWebChannel()
self.webView.page().setWebChannel(poChannel)
print(2)
objects = poChannel.registeredObjects()
print(objects)
poChannel.registerObject("editObject", editObject)
self.webView.setHtml("")
from PyQt5 import QtWebEngineWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is similar to Issues with PyQt5==5.10 on Ubuntu 18, but with example code. For the example code, https://gist.github.com/gioditalia/03c9fd5d793aeccbe065fea45d842431 is adapted.

The problem is that poChannel is a local variable that will be deleted after executing init_page, so in the second iteration the poChannel reference will point to an incorrect memory address. So the solution is to extend its cycle to that of the view, so we take advantage of the Qt property and pass it as a parent to self.webView.
poChannel = QtWebChannel.QWebChannel(self.webView)
Although as PyQt points out in the docs and the generated file: # WARNING! All changes made in this file will be lost!, it is not convenient to modify the class generated by the .ui, instead you must create another class that inherits from the appropriate widget and use the interface provided by Qt Designer.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
class EditObject(QtCore.QObject):
#QtCore.pyqtSlot(str)
def edit(self, s):
print("test")
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
for i in range(2):
self.init_page()
def init_page(self):
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
if poChannel is None:
poChannel = QtWebChannel.QWebChannel(self)
self.webView.page().setWebChannel(poChannel)
objects = poChannel.registeredObjects()
poChannel.registerObject("editObject", editObject)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Related

Updating label text from another class

I'm new at python and i need some help ;-)
I created a window with a label with the QT designer en generated the py file (window.py):
'''
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(847, 283)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lblMain = QtWidgets.QLabel(self.centralwidget)
self.lblMain.setGeometry(QtCore.QRect(160, 60, 311, 51))
self.lblMain.setObjectName("lblMain")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 847, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lblMain.setText(_translate("MainWindow", "label main window"))
'''
I created main.py which calls the window:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
So far so good.
From within main.py i can set the text of the label using:
self.ui.lblMain.setText('some text')
This works also.
Now i would like to create another file with another class which can update the label.
class update.py:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def settext(self):
self.ui.lblMain.setText('updated')
And here i'm stuck.
Can anyone give a hand ?
Cheers John
A function to update a label no matter where it came from:
def update_label(label, new_text):
label.setText(new_text)
You can save this function anywhere you like including inside a new class. The function or the new class don't need to know anything about the ui to do this.
If it's in a class you'll have to create an instance of that class before using it.
The class you show should work perfectly, simply call the function settext somewhere.
Under update.py:
class Updater:
def __init__(self, label):
self.label = label
def settext(self):
self.label.setText('updated')
Notice that this class is not another window, it accepts the label object from the Ui_MainWindow class as an argument and saves it as a property. To use it add under main, after you create the app:
my_instance = Updater(win.ui.lblMain)
my_instance.settext()
You could even pass the whole win.ui as an argument. As long as all the classes share the same instance of ui then they will change the same widgets
For completeness, a full program that uses the updater class to change the text in the GUI:
from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Updater:
def __init__(self, label):
self.label = label
def settext(self):
self.label.setText('updated')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = window()
win.show()
my_instance = Updater(win.ui.lblMain)
my_instance.settext()
sys.exit(app.exec_())

PyQt5 QgraphicsView width and height

file:main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 798, 598))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setSpacing(0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setMinimumSize(QtCore.QSize(0, 0))
self.graphicsView.setObjectName("graphicsView")
self.gridLayout_2.addWidget(self.graphicsView, 0, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
file: run_me.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from main import Ui_MainWindow as Ui_MainWindow
import sys
class Main_Code:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.showMaximized()
self.set_input_data()
self.set_view_variables()
self.print_lines()
sys.exit(self.app.exec_())
def set_input_data(self):
self.schedule_items = []
self.schedule_items.append({"title":"Title for schedule item 1","duration milliseconds":1000*51})
self.schedule_items.append({"title":"Title for schedule item 2","duration milliseconds":1000*120})
self.schedule_items.append({"title":"Title for schedule item 3","duration milliseconds":1000*500})
self.live_duration = 1000*60*60*2.5 #2 hours and 30 minutes
def set_view_variables(self):
self.header_timeline_height = 40
self.schedule_items_height_spacing = 50
self.schedule_item_timeline_height = 40
self.schedule_item_movable_height = 120
#get QGraphicsView dimensions
self.ui.graphicsView.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.available_width = self.ui.graphicsView.width()
self.available_height = self.ui.graphicsView.height()
self.ui.graphicsView.resize(self.available_width, self.available_height)
def print_lines(self):
#print timeline line (top)
self.MainWindow.setStyleSheet("background-color: black;")
self.ui.graphicsView.setStyleSheet("background-color: yellow;")
self.scene = QtWidgets.QGraphicsScene()
self.scene.setBackgroundBrush(QtGui.QBrush(Qt.red))
self.ui.graphicsView.setScene(self.scene)
self.ui.graphicsView.setSceneRect(0,0,self.available_width,self.available_height)
self.timeline_line_top = QtWidgets.QGraphicsLineItem(0,self.MainWindow.height(),self.available_width,0)
self.scene.addItem( self.timeline_line_top )
program = Main_Code()
I am trying to draw a line from the bottom left corner to the top right corner.
With the above code the output is:
I think this is maybe a scale problem (variables self.available_width,self.available_height).
What I am doing wrong?
The resize (and many other geometric properties) are not updated synchronously but rather the flags (such as maximezed) or the required size (using resize() method) are sent to the OS (through a library) to modify the existing window and then the OS implements it and returns the values it got after applying the changes. So it is not advisable to do calculations instantly but to give it a delay:
from PyQt5 import QtCore, QtGui, QtWidgets
from main import Ui_MainWindow as Ui_MainWindow
import sys
class Main_Code:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.showMaximized()
self.init_ui()
sys.exit(self.app.exec_())
def init_ui(self):
self.ui.graphicsView.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
self.MainWindow.setStyleSheet("background-color: black;")
self.ui.graphicsView.setStyleSheet("background-color: yellow;")
self.scene = QtWidgets.QGraphicsScene()
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.red))
self.ui.graphicsView.setScene(self.scene)
QtCore.QTimer.singleShot(100, self.draw_line)
def draw_line(self):
r = self.ui.graphicsView.mapToScene(
self.ui.graphicsView.viewport().rect()
).boundingRect()
self.timeline_line_top = QtWidgets.QGraphicsLineItem(
QtCore.QLineF(r.bottomLeft(), r.topRight())
)
self.scene.addItem(self.timeline_line_top)
program = Main_Code()

How to access a widget in a QMainWindow from a QDialog

Before posting my question I searched a lot about it and I found some questions that might be similar but they do not solve my problem. I believe it is quite easy but I don't know how:
below is a minimal example of the problem:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.firstPushButton.clicked.connect(self.showDialog)
# the other stuff related to layout setup is ommited
def showDialog(self):
dialog = MyDialog(MainWindow)
dialog.exec()
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.writeHello)
def writeHello(self):
# How can I access firstLineEdit from MainWindow? I want to write "Hello" to the firstLineEdit
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setWindowTitle("BEM Analysis for propellers")
MainWindow.show()
sys.exit(app.exec())
Could you please tell me how can I implement writeHello() method in order to write something in firstLineEdit in the MainWindow
Thanks
First of all you should not modify the code generated by Qt Designer since it is not a GUI, it is just a class that fills a GUI, and that brings several inconveniences such as not being able to overwrite the methods of the widget, or some you want to use methods of the widget in that class. Instead it inherits from a suitable widget and uses the other class as an interface.
Going to the point you should not mix the classes since there will be a lot of dependency between them and in the future if you modify one class you will have to modify the other which is unbeatable, instead you use the signals to notify any change or action.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
MainWindow.setWindowIcon(QtGui.QIcon(':/icons/drone.ico'))
MainWindow.setIconSize(QtCore.QSize(32, 32))
self.centralwidget = QtWidgets.QWidget(MainWindow)
MainWindow.setCentralWidget(self.centralwidget)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.verticalLayout_2.addWidget(self.firstPushButton)
self.verticalLayout_2.addWidget(self.firstLineEdit)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.firstPushButton.clicked.connect(self.showDialog)
def showDialog(self):
dialog = MyDialog()
dialog.clicked.connect(self.writeHello)
dialog.exec()
#QtCore.pyqtSlot()
def writeHello(self):
self.firstLineEdit.setText('Hello')
class MyDialog(QtWidgets.QDialog):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.clicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.myButton)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.setWindowTitle("BEM Analysis for propellers")
w.show()
sys.exit(app.exec())

PyQt5 - cannot create new widgets dynamically on button press to function [duplicate]

I have a method named 'test()' that loads 3 one-row tables into a scrollbar.
For some reason that I cannot figure out, however, the while it works if I simply activate test() on load, it doesn't work if I comment it out and then try to activate it via the push of a button.
Here is the main module (with test())
from PyQt5 import QtCore, QtWidgets
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
#test(self)
def test(self):
from random import randint
x = randint(0, 99)
print(x)
height = 30
yPos = 0
for i in range(3):
rowVals = ['test%s' % str(x + i)]
qTbl = QtWidgets.QTableWidget(self.sawDoc)
qTbl.setObjectName("tbl%s" % (i))
qTbl.setGeometry(QtCore.QRect(0, yPos, 880, height))
qTbl.horizontalHeader().setVisible(False)
qTbl.verticalHeader().setVisible(False)
yPos += height
qTbl.setRowCount(1)
qTbl.setColumnCount(len(rowVals))
for r, cell in enumerate(rowVals):
item = QtWidgets.QTableWidgetItem()
item.setText(str(cell))
qTbl.setItem(0, r, item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
And here is the design module (with the button)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(896, 453)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.scrDoc = QtWidgets.QScrollArea(self.centralwidget)
self.scrDoc.setGeometry(QtCore.QRect(0, 0, 891, 391))
self.scrDoc.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrDoc.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrDoc.setWidgetResizable(False)
self.scrDoc.setObjectName("scrTest")
self.sawDoc = QtWidgets.QWidget()
self.sawDoc.setGeometry(QtCore.QRect(0, 0, 869, 300))
self.sawDoc.setObjectName("sawDoc")
self.scrDoc.setWidget(self.sawDoc)
self.btnTest = QtWidgets.QPushButton(self.centralwidget)
self.btnTest.setGeometry(QtCore.QRect(430, 400, 80, 15))
self.btnTest.setObjectName("btnTest")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.btnTest.clicked.connect(self.test)
def test(self):
import main
main.test(self)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "昊珩のCAT工具"))
self.btnTest.setText(_translate("MainWindow", "Test"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Simply nothing happens when I push the button (except for the print out working successfully).
Can someone tell what's wrong?
The parents show the children at the beginning, but afterwards it is the responsibility of the children to show themselves, the simple solution is to use show():
qTbl = QtWidgets.QTableWidget(self.sawDoc)
qTbl.show()
But I see that you are implementing the solution in an inelegant way, the connection do it in main.py, do not modify the file generated by Qt Designer (delete the connection and the test method in design.py)
You must use a layout and there add the QTableWidget.
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.btnTest.clicked.connect(self.test)
def test(self):
from random import randint
x = randint(0, 99)
lay = QtWidgets.QVBoxLayout(self.sawDoc)
for i in range(3):
rowVals = ['test%s' % str(x + i)]
qTbl = QtWidgets.QTableWidget()
qTbl.setObjectName("tbl%s" % (i))
qTbl.horizontalHeader().hide()
qTbl.verticalHeader().hide()
qTbl.setRowCount(1)
qTbl.setColumnCount(len(rowVals))
for r, cell in enumerate(rowVals):
item = QtWidgets.QTableWidgetItem()
item.setText(str(cell))
qTbl.setItem(0, r, item)
qTbl.resize(qTbl.sizeHint())
lay.addWidget(qTbl)

Connection between from QDialog to QMainWindow - PyQt5

I have created two widgets (QMainWindow as win_one and QDialog as win_two) with qtdesigner and PyQt5.
From win_one, I open win_two, fill-in the lineEdit and press OK to transfer the entry into a label displayed in win_one. Everything works well except two problems:
win_one window is opened as .showMaximized() but after filled-in the label, the dimension of the window changes.
the button from win_one stops to work
front_win_one.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_win_one(object):
def setupUi(self, win_one):
win_one.setObjectName("win_one")
win_one.resize(1147, 234)
self.centralwidget = QtWidgets.QWidget(win_one)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 50, 111, 51))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 160, 131, 31))
self.label.setObjectName("label")
win_one.setCentralWidget(self.centralwidget)
self.retranslateUi(win_one)
QtCore.QMetaObject.connectSlotsByName(win_one)
def retranslateUi(self, win_one):
_translate = QtCore.QCoreApplication.translate
win_one.setWindowTitle(_translate("win_one", "MainWindow"))
self.pushButton.setText(_translate("win_one", "To qdialog"))
self.label.setText(_translate("win_one", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win_one = QtWidgets.QMainWindow()
ui = Ui_win_one()
ui.setupUi(win_one)
win_one.show()
sys.exit(app.exec_())
front_win_two.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_win_two(object):
def setupUi(self, win_two):
win_two.setObjectName("win_two")
win_two.resize(317, 278)
self.pushButton = QtWidgets.QPushButton(win_two)
self.pushButton.setGeometry(QtCore.QRect(40, 120, 121, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(win_two)
self.lineEdit.setGeometry(QtCore.QRect(30, 50, 161, 21))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(win_two)
QtCore.QMetaObject.connectSlotsByName(win_two)
def retranslateUi(self, win_two):
_translate = QtCore.QCoreApplication.translate
win_two.setWindowTitle(_translate("win_two", "Dialog"))
self.pushButton.setText(_translate("win_two", "OK"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win_two = QtWidgets.QDialog()
ui = Ui_win_two()
ui.setupUi(win_two)
win_two.show()
sys.exit(app.exec_())
back.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QDialog
from front_win_1 import Ui_win_one
from front_win_2 import Ui_win_two
class win_two(QDialog, Ui_win_two):
def __init__(self, parent=None):
super(win_two, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.vers_main)
def vers_main(self):
entry = self.lineEdit.text()
win_one().label.setText(entry)
class win_one(QMainWindow, Ui_win_one):
def __init__(self, parent=None):
super(win_one, self).__init__(parent)
self.setupUi(dialog)
self.pushButton.clicked.connect(self.open_qdialog)
def open_qdialog(self):
self.dialog_win_2 = win_two()
self.dialog_win_2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = QMainWindow()
prog = win_one(dialog)
dialog.showMaximized()
sys.exit(app.exec_())
Thank you
Your code has some inconsistencies:
You should not do this dialog = QMainWindow(), since it is enough to create an object of the class win_one, for this you must change self.setupUi(dialog) to self.setupUi(self).
With the statement win_one().label.setText(entry) you are creating a new object, which is unnecessary, besides that you are losing the previous object so when you press the window again, QDialog is not opened, a simple solution is to pass it as parent to win_one to win_two and use the self.parent() function to access it.
All of the above is implemented in the following part:
class win_two(QDialog, Ui_win_two):
def __init__(self, parent=None):
super(win_two, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.vers_main)
def vers_main(self):
entry = self.lineEdit.text()
self.parent().label.setText(entry)
class win_one(QMainWindow, Ui_win_one):
def __init__(self, parent=None):
super(win_one, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.open_qdialog)
def open_qdialog(self):
self.dialog_win_2 = win_two(self)
self.dialog_win_2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
prog = win_one()
prog.showMaximized()
sys.exit(app.exec_())
Note: I could never reproduce the first bug, only the second one.

Categories