self.close and self.hide not working in pyqt5 - python

I am new to python. I am using pyqt5 for GUI development. I have a main Window which should close and new dialog appears after clicking the pushButton. But it doesnot closes and neither show any error and opens the nextDialog. I also want to close nextDialog when OK button is clicked in nextDialog. Please help to check the issue. I am trying to develeop a new project but stuck on this issue. Codes are given below.
Main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(370, 171)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.opennext)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
self.close() #Not working
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open"))
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_())
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
nextDialog.py
class Ui_Dialog(QtWidgets.QMainWindow):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(346, 182)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.exit)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def exit(self):
self.hide() #This also not working, I want either of these two to
#work
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "OK"))

Try it:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.centralwidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralwidget)
self.pushButton = QtWidgets.QPushButton("Open", self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.clicked.connect(self.opennext)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
self.close() #Not working
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
window.show()
sys.exit(app.exec_())
nextDialog.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(QtWidgets.QMainWindow):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(346, 182)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(Dialog.close) # <---
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "OK"))

Hi kind of difficult to understand your code, I am using just Designer to make my GUI,
in anycase I made the first part of the puzzle work for your main.py
line 4 should read class Ui_MainWindow(object):
and to close the first window about lane 25 use MainWindow.close()
added import sys too at the beginning, here the code
main.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 18 17:43:19 2020
#author: Pietro
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(370, 171)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.opennext)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
MainWindow.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
this open first window then close it when open buttton is pressed and new window appears, second part of the riddle is gonna take longer I'll try it as soon as I get some spare time
Dialog.py from answer below seems to work dont know why (dont know how main.py works too, but it is kind of more illogic to me the fact that def exit doesnt work).
nextDialog.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 18 17:43:19 2020
#author: Pietro
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(QtWidgets.QMainWindow):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(346, 182)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(Dialog.close) # as answer below dont know why def exit doesnt work
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# def exit(self):
# print('exit' *5)
# Dialog.close()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "OK"))

Related

pyqt5 send/show messages from different file/class

I have two files, "main.py", "debugger.py".
On "debugger.py" I have a "textEdit" where I want to get/show messages.
On "main.py" I import and instantiate the class from "debugger.py" but instead of just sending/showing the messages, it opens the "debugger.py" window UI and shows the messages, i just need to send the message without opening the window, how can I do that?
"debugger.py".
import os, time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QTextCursor
class Debugger_MainWindow(object):
def __init__(self):
super().__init__()
#def print_text(self):
#self.txtEdit_debugger.insertPlainText('hi')
def Debugger_setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbl_info = QtWidgets.QLabel(self.centralwidget)
self.lbl_info.setGeometry(QtCore.QRect(90, 450, 451, 61))
self.lbl_info.setObjectName("lbl_info")
self.lbl_tittle = QtWidgets.QLabel(self.centralwidget)
self.lbl_tittle.setGeometry(QtCore.QRect(280, 40, 56, 15))
self.lbl_tittle.setObjectName("lbl_tittle")
self.txtEdit_debugger = QtWidgets.QTextEdit(self.centralwidget)
#self.txtEdit_debugger.setEnabled(False)
self.txtEdit_debugger.setGeometry(QtCore.QRect(70, 80, 481, 361))
self.txtEdit_debugger.setObjectName("txtEdit_debugger")
self.txtEdit_debugger.setReadOnly(True)
##Check box on/off
self.chkb_on_off_debugger = QtWidgets.QCheckBox(self.centralwidget)
self.chkb_on_off_debugger.setGeometry(QtCore.QRect(90, 500, 30, 20))
self.chkb_on_off_debugger.setObjectName("chkb_on_off_debugger")
#self.chkb_on_off_debugger.stateChanged.connect(self.print_text)#Show logger messages
self.chkb_on_off_debugger.setCheckable(True)
self.lbl_on_off_debugger = QtWidgets.QLabel(self.centralwidget)
self.lbl_on_off_debugger.setGeometry(QtCore.QRect(110, 500, 60, 20))
self.lbl_on_off_debugger.setObjectName("lbl_on_off_debugger")
MainWindow.setCentralWidget(self.centralwidget)
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.lbl_info.setText(_translate("MainWindow", "This is read only mode. "))
self.lbl_tittle.setText(_translate("MainWindow", "Debugger"))
self.lbl_on_off_debugger.setText(_translate("MainWindow", "Show messages"))
self.lbl_on_off_debugger.adjustSize()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Debugger_MainWindow()
ui.Debugger_setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
"main.py"
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot, QThreadPool, QTimer
from PyQt5.QtGui import QColor, QKeySequence, QIcon, QTextCursor
from Debugger import Debugger_MainWindow
class Ui_MainWindow(object):
def __init__(self):
super().__init__()
###### Methods
#this method opens a new window, i just want to send the message
def print_text(self):
self.ui_debugger = Debugger_MainWindow()
self.ui_debugger.Debugger_setupUi(MainWindow)
self.ui_debugger.txtEdit_debugger.insertPlainText('hi')
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 500)
MainWindow.setStyleSheet(open("styles.qss", "r").read())
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
##Group box
self.gpb_spell_main = QtWidgets.QGroupBox(self.centralwidget)
self.gpb_spell_main.setGeometry(QtCore.QRect(10, 5, 290, 230))
self.gpb_spell_main.setObjectName("gpb_spell_main")
self.gpb_spell_main.setCheckable(True)
self.gpb_spell_main.setChecked(False)
##Button
spell_main_color = QColor(255, 0, 255)
self.btn_spell_main = QtWidgets.QPushButton(self.gpb_spell_main)
self.btn_spell_main.setGeometry(QtCore.QRect(10, 35, 85, 27))
self.btn_spell_main.setObjectName("btn_spell_main")
self.btn_spell_main.clicked.connect(self.print_text)
#self.btn_spell_main.setEnabled(False)
MainWindow.setCentralWidget(self.centralwidget)
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.gpb_spell_main.setTitle(_translate("HeallingWindow", "Spell Main"))
self.btn_spell_main.setText(_translate("MainWindow", "Click Here"))
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_())
Note: I couldn't fix my issue reading similar questions. PyQt5 - Show QDialog from a different class
I don't know if I understand problem but you may have to create new QtWidgets.QMainWindow() to create separated Debug Window. If you use existing MainWindow then it replaces widgets in existing window.
You may also set None to Debug Window in __init__ and later check if Debug Window is not None - to create only one window.
class Ui_MainWindow(object):
def __init__(self):
super().__init__()
self.DebugWindow = None
def print_text(self):
if self.DebugWindow is None:
self.DebugWindow = QtWidgets.QMainWindow()
self.ui_debugger = Debugger_MainWindow()
self.ui_debugger.Debugger_setupUi(self.DebugWindow)
self.DebugWindow.show()
self.ui_debugger.txtEdit_debugger.insertPlainText('hi\n')
Other problem cam be when you close Debug Window because it will not opne it again. It may need to change code in Debug Window and assign function which will hide it instead of destroing it.

pyqt5 closing a child window from a parent window

I have created 2 windows, win1 and win2, using qt designer and have added two buttons: btn_open_win2 to open win2 from win1 and btn_close to close win2.
When I run win2 directly the close method works correctly but when I run win1 and call win2 from it, the close button on win2 doesn't operate correctly.
I know that the problem arises from the point that win2 has been defined inside the if __name__ =='__main__' part as shown in the below codes but I can't solve this problem.
win1:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win1(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Win1")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_open_win2 = QtWidgets.QPushButton(self.centralwidget)
self.btn_open_win2.setGeometry(QtCore.QRect(250, 170, 131, 101))
self.btn_open_win2.setObjectName("btn_open_win2")
self.btn_open_win2.clicked.connect(self.on_open_win2)
MainWindow.setCentralWidget(self.centralwidget)
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", "Win1"))
self.btn_open_win2.setText(_translate("MainWindow", "open win2"))
def on_open_win2(self):
from win2 import Ui_Win2
self.win = QMainWindow()
self.ui = Ui_Win2()
self.ui.setupUi(self.win)
self.win.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win1 = QMainWindow()
ui = Ui_Win1()
ui.setupUi(win1)
win1.show()
sys.exit(app.exec_())
win2:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Win2")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_close = QtWidgets.QPushButton(self.centralwidget)
self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
self.btn_close.setObjectName("btn_close")
self.btn_close.clicked.connect(self.on_close)
MainWindow.setCentralWidget(self.centralwidget)
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", "Win2"))
self.btn_close.setText(_translate("MainWindow", "close"))
def on_close(self):
# win2 = QMainWindow()
# ui = Ui_Win2()
# ui.setupUi(win2)
win2.close()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win2 = QMainWindow()
ui = Ui_Win2()
ui.setupUi(win2)
win2.show()
sys.exit(app.exec_())
change win2 like this:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win2(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
MainWindow.setObjectName("Win2")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_close = QtWidgets.QPushButton(self.centralwidget)
self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
self.btn_close.setObjectName("btn_close")
self.btn_close.clicked.connect(self.on_close)
MainWindow.setCentralWidget(self.centralwidget)
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", "Win2"))
self.btn_close.setText(_translate("MainWindow", "close"))
def on_close(self):
# win2 = QMainWindow()
# ui = Ui_Win2()
# ui.setupUi(win2)
self.MainWindow.close()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win2 = QMainWindow()
ui = Ui_Win2()
ui.setupUi(win2)
win2.show()
sys.exit(app.exec_())
call close function from self.MainWindow instead of self. and set self.MainWindow in first line of setupUi function

Qtimer and closing out an applicaiton after 15 seconds have passed

Evening. I am having a bit of a hard time both understanding QTimer and how it works as well as getting the below code to work. As much as I hate copy/pasting, I just cannot figure it out. The goal of this is to run the program for 15 seconds and then exit out completely. Everything else works, but when I try to integrate QTimer to count how many seconds have passed, it does nothing or just simply does not work regardless of the variations I try. Below is the latest code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QTimer
import winsound
import time
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(321, 249)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 320, 248))
self.label.setFrameShape(QtWidgets.QFrame.Box)
self.label.setOpenExternalLinks(False)
self.label.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Plays the movie in the Label
movie = QMovie("giphy.gif")
self.label.setMovie(movie)
movie.start()
#Plays the sound with the movie
winsound.PlaySound("magicwrd.wav", winsound.SND_ASYNC|winsound.SND_LOOP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#Borrowed and modified from here: https://stackoverflow.com/questions/46656634/pyqt5-qtimer-count-until-specific-seconds
#Goal is to run the application for 15 seconds and then exit out completely.
def start_timer(self, slot, count=1, interval=1000):
counter = 0
def handler():
nonlocal counter
counter += 1
slot(counter)
if counter >= count:
timer.stop()
timer.deleteLater()
timer = QtCore.QTimer()
timer.timeout.connect(handler)
timer.start(interval)
def timer_func(self, count):
if count >= 5:
sys.exit(app.exec_())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Ui_MainWindow.start_timer(Ui_MainWindow.timer_func, 5)
app.exec_()
If you want to finish the application in T seconds then you must use a QTimer::singleShot() so that when it is triggered then QCoreApplication::quit() is invoked:
QtCore.QTimer.singleShot(T * 1000, QtCore.QCoreApplication.quit)
In the example that you link, it has another objective: to print some information every T seconds and after repeating K times the application is closed, and therefore uses a complicated logic that you do not need.
Considering the above and the PyQt5 recommendation, the solution is:
import sys
import winsound
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(321, 249)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 320, 248))
self.label.setFrameShape(QtWidgets.QFrame.Box)
self.label.setOpenExternalLinks(False)
self.label.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# Plays the movie in the Label
movie = QtGui.QMovie("giphy.gif")
self.label.setMovie(movie)
movie.start()
# Plays the sound with the movie
winsound.PlaySound("magicwrd.wav", winsound.SND_ASYNC | winsound.SND_LOOP)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
QtCore.QTimer.singleShot(15 * 1000, QtCore.QCoreApplication.quit)
sys.exit(app.exec_())

How to passing value from main to dialog

I'm newbie for using Pyqt5.
I use qtdesigner for building GUI.
I have MainWindow for passing value to dialogwindow
I want to LineEdit( in dialogwindow) show value after user input and click button (in MainWindow)
I try
self.ui = Ui_Dialog(self,data)
but it doesn't work
My code mainpage
MainWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
class Ui_MainWindow(object):
def openDialog(self):
data = self.lineEdit.text()
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog(self,data)
self.ui.setupUi(self.window)
# MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 236)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 40, 91, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 40, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 110, 93, 28))
self.pushButton.clicked.connect(self.openDialog)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 505, 26))
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.label.setText(_translate("MainWindow", "Passing Value"))
self.pushButton.setText(_translate("MainWindow", "Send"))
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_())
Dialog code.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(577, 253)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(260, 100, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(180, 100, 55, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Value"))
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_())
advice me plz
Thank you,
Mint
PyQt recommends not modifying the .py generated by pyuic and Qt Designer but creating another file that uses that class to fill a widget so I recommend regenerating the MainWindow.py and dialog.py files.
Now create a main.py where we inherit the appropriate class by setting a constructor with the requirement:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
from MainWindow import Ui_MainWindow
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, text, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText(text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
def openDialog(self):
data = self.lineEdit.text()
w = Dialog(data)
w.exec_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

how to call another window in pyqt4 python?

how to call another window in pytq4?
For example i have two separate form. I want to show the second form when i click a button in the login form.
EDITED:
for example i have this dialog for my login.
when the user click a button the main.py would appear and this dialog will be close.
LOGIN.py
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(379, 184)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(20, 130, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
MAIN.py
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(595, 315)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
I had a similar situation and this is what I've done. Does not feel like the ideal solution but it worked for me.
import Main
self.main = Main.Form(user, loginWindow=self)
self.hide()
self.main.show()
# You can do self.close() here OR
# loginWindow.close() from Main.py
self.close()
Main.py
class Form(QWidget):
def __init__(self, user, parent=None, loginWindow=None):
super(Form, self).__init__(parent)
# etc...

Categories

Resources