Defined a custom widget by extending a built-in QLineEdit by making it 'Double-Clickable':
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, type, parent=None):
super(DCLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
Now I can use this 'custom' Clickable_LineEdit widget to populate a main dialog window. It all works fine. But every time a double-click occurs it is registered OUTSIDE of main dialog window class... meaning all the main_dialog class's variables, all the data left behind and it is not available to Clickable_LineEdit class instance.
I wonder if it's possible to get doubleClick functionality from LineEdit without leaving a main dialog class.
EDITED LATER:
Here is modified example from a post below:
from PyQt4 import QtCore, QtGui
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(QtGui.QLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
class Ui_Dialog(QtGui.QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(348, 195)
self.lineEdit = Clickable_LineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
def runOnDoubleClick(self):
print "DOUBLE CLICK"
import sys
def main():
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.setupUi(ex)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I have added a "runOnDoubleClick" function under a "Ui_Dialog()" class I would like to run every time a self.lineEdit widget is double-clicked (instead of mouseDoubleClickEvent() function). How would it be accomplished?
Well , according to what i understood , you want to a lineEdit that senses a doubleclick signal whenever lineEdit is doubleClicked .
from PyQt4 import QtCore, QtGui
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(QtGui.QLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
class Ui_Dialog(QtGui.QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(348, 195)
self.lineEdit = Clickable_LineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
import sys
def main():
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.setupUi(ex)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This seems to work for me .
Related
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_())
I have a problem, i have made a QGraphicsView with 2 rectangles inside. I wanted them to resize when i resize the Dialog so i rewrite a dialog class. It works but at start i have the wrong sized rectangle
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsEllipseItem
import sys
# Redéfinition de dialog pour utiliser resize event
class MyDialog (QtWidgets.QDialog) :
def resizeEvent(self, event):
print("resize")
#print(view.sceneRect())
view.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio) ######## I DONT KNOW WHY THIS LINE MAKE THE VIEW TOO SMALL AT START
QtWidgets.QDialog.resizeEvent(self, event)
#view.fitInView(rectangle)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(556, 580)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.graphicsView = QtWidgets.QGraphicsView(Dialog)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
global view
view = self.graphicsView
triangle = QtWidgets.QGraphicsRectItem(10,10,200,200)
rectangle = QtWidgets.QGraphicsRectItem(100,100,200,200)
global scene
scene = QtWidgets.QGraphicsScene()
scene.addItem(rectangle)
scene.addItem(triangle)
self.graphicsView.setScene(scene)
#self.graphicsView.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
#view.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
self.graphicsView_2 = QtWidgets.QGraphicsView(Dialog)
self.graphicsView_2.setObjectName("graphicsView_2")
self.verticalLayout.addWidget(self.graphicsView_2)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout.addWidget(self.buttonBox)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = MyDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
When i start the QDialog it looks like this
And when i resize the window it looks like i wanted it to look like
Note: It is recommended not to modify the code generated by Qt Designer, so for my answer to work you must regenerate the file using pyuic5: pyuic5 your_file.ui -o gui.py -x.
So that the fitInView method uses some property that is not updated until it makes the QGraphicsView visible, so the solution is to verify that it is visible:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from gui import Ui_Dialog
class MyDialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
triangle = QtWidgets.QGraphicsRectItem(10, 10, 200, 200)
rectangle = QtWidgets.QGraphicsRectItem(100, 100, 200, 200)
scene = QtWidgets.QGraphicsScene()
scene.addItem(rectangle)
scene.addItem(triangle)
self.graphicsView.setScene(scene)
def resizeEvent(self, event):
if self.isVisible():
self.graphicsView.fitInView(
self.graphicsView.scene().sceneRect(), QtCore.Qt.KeepAspectRatio
)
super().resizeEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MyDialog()
w.show()
sys.exit(app.exec_())
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())
I am trying to get stdout and error messages to show on my main window. The window is by pyqt and made via designer. I have a QTextEdit on it. This is where the output should show itself. Also I have a dialog box (again made via designer) where i set some settings for my program before running it. The dialog box is opened like this:
def open_settings(self):
dialog = SettingsDialog()
dialog.open_settings_tab() # its on a tab widget
I already read and used the info on these links to achieve my goal:
Print out python console output to Qtextedit
How to capture output of Python's interpreter and show in a Text widget?
Both are pretty much the same with different object names. The issue I'm having is that whenever I open a dialog box and return to the main window the stdout no longer shows itself on the QTextEdit. Instead it goes back to showing itself on Sublime Editor.
I believe it has something to do with the class instancing.
Here is how the Dialog class starts:
class SettingsDialog(QDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.ui = Ui_SettingsDialog()
self.ui.setupUi(self)
and finally here is how my main window (form) class starts:
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
# Install the custom output stream
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
self.ui = Ui_MyForm()
self.ui.setupUi(self)
Any ideas of why the stdout stops working (in qtextedit) once i go into the dialog screen and come back?
NEW Update:
The code is very long. I made a small program thats showing the issue:
PS: I found that the problem is related with this line shown below:
self.ui.pushButton_path.clicked.connect(Form(self).change_path)
if i comment it out the problem goes away.. But I need to call that function (which opens a QDialog, from the main form). What is the proper way?
main:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QTextCursor
from ui_form import Ui_Form
from ui_dialog import Ui_Dialog
class EmittingStream(QObject): # test
textWritten = pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class Form(QMainWindow):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Install the custom output stream
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten) # test
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton_open.clicked.connect(self.open_dialog)
self.ui.pushButton_text.clicked.connect(self.test_write)
def __del__(self): # test
# Restore sys.stdout
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text): # test
"""Append text to the QTextEdit."""
# Maybe QTextEdit.append() works as well, but this is how I do it:
# self.ui.tEdit_cli.insertPlainText(text)
cursor = self.ui.textEdit.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.ui.textEdit.setTextCursor(cursor)
self.ui.textEdit.ensureCursorVisible()
def open_dialog(self):
dialog = Dialog()
dialog.open_tab()
def test_write(self):
print("something written")
def change_path(self):
pass
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton_close.clicked.connect(self.close_dialog)
self.ui.pushButton_path.clicked.connect(Form(self).change_path) # this is what causes the issue. but i need to use it!
def open_tab(self):
self.ui.tabWidget.setCurrentIndex(0)
self.exec_()
def close_dialog(self):
self.close()
def main():
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
ui_dialog:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout.setObjectName("horizontalLayout")
self.tabWidget = QtWidgets.QTabWidget(Dialog)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.pushButton_close = QtWidgets.QPushButton(self.tab)
self.pushButton_close.setGeometry(QtCore.QRect(100, 80, 211, 131))
self.pushButton_close.setObjectName("pushButton_close")
self.pushButton_path = QtWidgets.QPushButton(self.tab)
self.pushButton_path.setGeometry(QtCore.QRect(30, 30, 75, 23))
self.pushButton_path.setObjectName("pushButton_path")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.horizontalLayout.addWidget(self.tabWidget)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_close.setText(_translate("Dialog", "close"))
self.pushButton_path.setText(_translate("Dialog", "path"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Tab 2"))
"""
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
"""
ui_form:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(Form)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(90, 230, 601, 271))
self.textEdit.setObjectName("textEdit")
self.pushButton_open = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_open.setGeometry(QtCore.QRect(140, 80, 241, 81))
self.pushButton_open.setObjectName("pushButton_open")
self.pushButton_text = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_text.setGeometry(QtCore.QRect(440, 80, 251, 81))
self.pushButton_text.setObjectName("pushButton_text")
Form.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Form)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
Form.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Form)
self.statusbar.setObjectName("statusbar")
Form.setStatusBar(self.statusbar)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "MainWindow"))
self.pushButton_open.setText(_translate("Form", "open dialog"))
self.pushButton_text.setText(_translate("Form", "write somthing"))
"""
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
"""
You must use the object to invoke a method, you must not use the class, so the instruction Form(self) is not valid.
You must make the connection where you can access the signal and the slot simultaneously, for example open_dialog would be a good place:
class Form(QMainWindow):
...
def open_dialog(self):
dialog = Dialog(self)
dialog.ui.pushButton_path.clicked.connect(self.change_path) # +++
dialog.open_tab()
def test_write(self):
print("something written")
def change_path(self):
pass
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton_close.clicked.connect(self.close_dialog)
# self.ui.pushButton_path.clicked.connect(Form(self).change_path) ---
...
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.