This question already has answers here:
Close and Open new Window PYQT5
(2 answers)
Closed 3 years ago.
I am currently trying to get a button press in one qt window to open a second window which is in another python file. My layout code was generated by converting a ui made in Qt designer to python.
My select feature code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'movie_design.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import add_entry
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(597, 522)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 40, 291, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(False)
font.setUnderline(True)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(True)
self.label.setFont(font)
self.label.setLineWidth(1)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 110, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(170, 230, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(170, 350, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 597, 26))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_2.clicked.connect(self.add)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Movie Database Program"))
self.pushButton.setText(_translate("MainWindow", "Delete Record"))
self.pushButton_2.setText(_translate("MainWindow", "Add Record"))
self.pushButton_3.setText(_translate("MainWindow", "Search Records"))
self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
def add(self):
add_entry.add_entry()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
if __name__ == "__main__":
main()
As you can see, I have created a button that when pressed calls the function in the program "add_entry.py"
Add entry.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'add_entry.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# 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(555, 418)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 30, 533, 28))
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setScaledContents(False)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.formLayoutWidget = QtWidgets.QWidget(Dialog)
self.formLayoutWidget.setGeometry(QtCore.QRect(30, 70, 501, 321))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.MovieTitle = QtWidgets.QLineEdit(self.formLayoutWidget)
self.MovieTitle.setObjectName("MovieTitle")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.MovieTitle)
self.label_3 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.MovieStudio = QtWidgets.QLineEdit(self.formLayoutWidget)
self.MovieStudio.setObjectName("MovieStudio")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.MovieStudio)
self.label_4 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_4)
self.dateEdit = QtWidgets.QDateEdit(self.formLayoutWidget)
self.dateEdit.setObjectName("dateEdit")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.dateEdit)
self.label_5 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_5)
self.ProductionCost = QtWidgets.QSpinBox(self.formLayoutWidget)
self.ProductionCost.setMaximum(1000000000)
self.ProductionCost.setObjectName("ProductionCost")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.ProductionCost)
self.label_6 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_6)
self.BoxOfficeRevenue = QtWidgets.QSpinBox(self.formLayoutWidget)
self.BoxOfficeRevenue.setMaximum(1000000000)
self.BoxOfficeRevenue.setObjectName("BoxOfficeRevenue")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.BoxOfficeRevenue)
self.label_7 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_7)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.No = QtWidgets.QRadioButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.No.setFont(font)
self.No.setObjectName("No")
self.horizontalLayout.addWidget(self.No)
self.Yes = QtWidgets.QRadioButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.Yes.setFont(font)
self.Yes.setObjectName("Yes")
self.horizontalLayout.addWidget(self.Yes)
self.formLayout.setLayout(5, QtWidgets.QFormLayout.FieldRole, self.horizontalLayout)
self.label_8 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.label_8)
self.classificationBox = QtWidgets.QComboBox(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.classificationBox.setFont(font)
self.classificationBox.setObjectName("classificationBox")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.classificationBox)
self.pushButton = QtWidgets.QPushButton(self.formLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setObjectName("pushButton")
self.formLayout.setWidget(7, QtWidgets.QFormLayout.SpanningRole, self.pushButton)
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", "<html><head/><body><p><span style=\" font-size:12pt; text-decoration: underline;\">Add Entry</span></p></body></html>"))
self.label_2.setText(_translate("Dialog", "Movie Title"))
self.label_3.setText(_translate("Dialog", "Movie Studio"))
self.label_4.setText(_translate("Dialog", "Release Date"))
self.label_5.setText(_translate("Dialog", "Production Cost (m$)"))
self.label_6.setText(_translate("Dialog", "Box Office Revenue (m$)"))
self.label_7.setText(_translate("Dialog", "Have you seen this movie?"))
self.No.setText(_translate("Dialog", "Yes"))
self.Yes.setText(_translate("Dialog", "No"))
self.label_8.setText(_translate("Dialog", "Classification (Age Rating)"))
self.classificationBox.setItemText(0, _translate("Dialog", "U"))
self.classificationBox.setItemText(1, _translate("Dialog", "PG"))
self.classificationBox.setItemText(2, _translate("Dialog", "12"))
self.classificationBox.setItemText(3, _translate("Dialog", "15"))
self.classificationBox.setItemText(4, _translate("Dialog", "18"))
self.pushButton.setText(_translate("Dialog", "Add Entry"))
def add_entry():
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
app.exec_()
if __name__ == "__main__":
add_entry()
The issue is that the when the button is pressed, the program crashes, telling me that "Event loop already running". How do I fix this?
Qt, like many GUI frameworks, is based on an event loop which is started with app.exec_() and then waits for events to react to (both from the user or the system).
For each Qt program there should always be a single and only QApplication instance running its event loop.
With your implementation, each time you press the button, you're trying to create and launch a new QApplication, and Qt will not allow you for the reason above.
Nonetheless, the problem with your code is somewhere else, as you are using the generated UI code in the wrong way: those files should never be used as a basis to create your program code (in fact, you should always leave them as they are).
I suggest you to carefully read the official documentation about using Designer to understand how to use those files.
Regenerate the UI files with pyuic, and then create a new script that will probably contain something similar to this:
from PyQt5 import QtWidgets
from ui_mainwindow import Ui_MainWindow
from ui_dialog import Ui_Dialog
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton_2.clicked.connect(self.add)
def add(self):
dialog = Dialog(self)
dialog.exec_()
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
In this example I used the third method (multiple inheritance approach) explained in the documentation given above.
Related
I have created a manual web socket and I stucked.
Query) How to print continous/real time data receiving from server on text browser in pyqt5 GUI if connect (here name close) pushbutton is pressed (connection created) and if pushbutton is pressed again it should get toggled i.e. it should stop receiving data (connection close).
My code is
from PyQt5 import QtCore, QtGui, QtWidgets
from websocket import create_connection
class Ui_MainWindow(object):
def connect(self,MainWindow):
ws = create_connection("ws://192.168.0.205/ws")
self.textBrowser_2.append("Connection created")
while 1:
self.textBrowser_2.append(ws.recv())
print(ws.recv())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(700, 70, 81, 61))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 0, 191, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 60, 81, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(160, 130, 141, 81))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 200, 201, 61))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(690, 260, 81, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(20, 330, 251, 21))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(82, 70, 601, 61))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 250, 651, 71))
self.lineEdit_2.setObjectName("lineEdit_2")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(260, 150, 381, 51))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(30, 360, 731, 192))
self.textBrowser_2.setObjectName("textBrowser_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.connect)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Close"))
self.label.setText(_translate("MainWindow", "Server loaction"))
self.label_2.setText(_translate("MainWindow", "URL"))
self.label_3.setText(_translate("MainWindow", "Status :"))
self.label_4.setText(_translate("MainWindow", "Request"))
self.pushButton_2.setText(_translate("MainWindow", "Send"))
self.label_5.setText(_translate("MainWindow", "Message Log"))
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_())
First of all, do not modify the code generated by QtDesigner so to apply my solution you must regenerate the .py file and call it gui.py: pyuic5 filename.ui -o gui.py -x.
On the other hand, do not use the websocket module since it is designed for synchronous logic, in this case it is better to use the QtWebSockets module that allows you to interact with the websockets using the Qt eventloop.
import sys
from PyQt5 import QtCore, QtWidgets, QtWebSockets
from gui import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.socket = QtWebSockets.QWebSocket()
self.socket.textMessageReceived.connect(self.handle_text_message_received)
self.ui.pushButton.clicked.connect(self.connect_to_websocket)
def connect_to_websocket(self):
if self.socket.state() == QtNetwork.QAbstractSocket.UnconnectedState:
url = "ws://192.168.0.205/ws"
self.socket.open(QtCore.QUrl(url))
elif self.socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
self.socket.close()
def handle_text_message_received(self, message):
self.ui.textBrowser_2.append(message)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I have created two Windows in QT designer. The first one has two pushButtons, each one calls the Dialog window. I am just starting to write the code for this but I could not go further when I tested the pushbutton on the dialog window. I have just connected to a function which calls print ("test."), Just to see the PB works, but it doesn't work.what I am missing?
See the main code below:
from PyQt5 import QtWidgets
from Main_VIEW import Ui_MainWindow # this is the main window
from Dialog_sa import Ui_Dialog # dialog window
from Test4 import currentAccount,savingAccount # this has all functions and calculations
import sys
class ApplicationWindow(QtWidgets.QMainWindow,Ui_MainWindow,Ui_Dialog,QtWidgets.QDialog):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#self.diag_ui = Ui_Dialog()
#self.diag_ui.setupUi(self)
self.ui.pb_ca.clicked.connect(self.open_dialog_ca)
def open_dialog_ca(self):
#global dialog
dialog = QtWidgets.QDialog()
dialog.ui = Ui_Dialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
account = currentAccount()
dialog.value_str = dialog.ui.le_b.text()
dialog.ui.pb_d.clicked.connect(self.print_sel) # this is the line of code that doesn't work
def print_sel(self):
print("this app")
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
this is Dialog_sa
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Dialog_sa.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(301, 142)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(-70, 110, 341, 32))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.buttonBox.setFont(font)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.le_b = QtWidgets.QLineEdit(Dialog)
self.le_b.setGeometry(QtCore.QRect(50, 40, 131, 61))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(18)
self.le_b.setFont(font)
self.le_b.setObjectName("le_b")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 20, 47, 13))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pb_w = QtWidgets.QPushButton(Dialog)
self.pb_w.setGeometry(QtCore.QRect(190, 80, 75, 23))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pb_w.setFont(font)
self.pb_w.setObjectName("pb_w")
self.pb_d = QtWidgets.QPushButton(Dialog)
self.pb_d.setGeometry(QtCore.QRect(190, 40, 75, 23))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pb_d.setFont(font)
self.pb_d.setObjectName("pb_d")
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"))
self.label.setText(_translate("Dialog", "Balance"))
self.pb_w.setText(_translate("Dialog", "Withdraw"))
self.pb_d.setText(_translate("Dialog", "Deposit"))
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_())
This is MAIN_VIEW
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_VIEW.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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(259, 147)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pb_ca = QtWidgets.QPushButton(self.centralwidget)
self.pb_ca.setGeometry(QtCore.QRect(50, 10, 171, 41))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pb_ca.setFont(font)
self.pb_ca.setObjectName("pb_ca")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(50, 60, 171, 41))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 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.pb_ca.setText(_translate("MainWindow", "Current Account"))
self.pushButton_2.setText(_translate("MainWindow", "Saving Account"))
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_())
Using PyQt5 QPlaintext to store variables. Example AO = userinput?
What I have so far minus the GUI setup. I need to store it as simple variables so i can output to a DOCX. I know I have to manually user input for every QPlaintextbox available. would I use the getText(), link it to the push button to generate the variables. Thank you for your time.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1340, 922)
self.OPORD = QtWidgets.QTabWidget(Dialog)
self.OPORD.setGeometry(QtCore.QRect(0, 0, 1231, 991))
self.OPORD.setTabBarAutoHide(False)
self.OPORD.setObjectName("OPORD")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.label = QtWidgets.QLabel(self.tab_4)
self.label.setGeometry(QtCore.QRect(280, 240, 311, 331))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("WAATS_Final.png"))
self.label.setObjectName("label")
self.OPORD.addTab(self.tab_4, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.ORDERS = QtWidgets.QStackedWidget(self.tab)
self.ORDERS.setGeometry(QtCore.QRect(0, 0, 1141, 1080))
self.ORDERS.setMaximumSize(QtCore.QSize(1920, 1080))
self.ORDERS.setObjectName("ORDERS")
self.stackedWidgetPage1 = QtWidgets.QWidget()
self.stackedWidgetPage1.setObjectName("stackedWidgetPage1")
self.WAATS4 = QtWidgets.QCheckBox(self.stackedWidgetPage1)
self.WAATS4.setGeometry(QtCore.QRect(390, 50, 141, 20))
self.WAATS4.setMaximumSize(QtCore.QSize(1920, 1080))
self.WAATS4.setObjectName("WAATS4")
self.label_2 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_2.setGeometry(QtCore.QRect(0, 70, 271, 21))
self.label_2.setMaximumSize(QtCore.QSize(1920, 1080))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_9 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_9.setGeometry(QtCore.QRect(240, 0, 181, 21))
self.label_9.setMaximumSize(QtCore.QSize(1920, 1080))
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.num = QtWidgets.QLineEdit(self.stackedWidgetPage1)
self.num.setGeometry(QtCore.QRect(0, 30, 181, 31))
self.num.setMaximumSize(QtCore.QSize(1920, 1080))
self.num.setObjectName("num")
self.aoi = QtWidgets.QPlainTextEdit(self.stackedWidgetPage1)
self.aoi.setGeometry(QtCore.QRect(0, 110, 431, 71))
self.aoi.setMaximumSize(QtCore.QSize(1920, 1080))
self.aoi.setObjectName("aoi")
self.COPORD = QtWidgets.QPushButton(self.widget)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.OPORD.setTabText(self.OPORD.indexOf(self.tab_4), _translate("Dialog", "Welcome"))
self.WAATS4.setText(_translate("Dialog", "TASS"))
self.label_2.setText(_translate("Dialog", "Situation"))
self.num.setPlaceholderText(_translate("Dialog", "##-##"))
self.aoi.setPlainText(_translate("Dialog", "?"))
self.aoi.setPlaceholderText(_translate("Dialog", "AI"))
self.label_3.setText(_translate("Dialog", "Area of Interest"))
self.AO.setText(_translate("Dialog", "Area of Operation"))
self.ao.setPlaceholderText(_translate("Dialog", "AO"))
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_())
as I see on your code, to get string from the QLineEdit you can use this code.
self.num.text()
if you print that code, it will automatically print the inputed string of QLineEdit.
you can add a QPushButton then make a signal slot to do that as a form in commonly use.
like these:
self.button = QPushButton()
self.button.clicked.connect(self.getLineEdit)
def getLineEdit(self):
lineEditText = self.num.text()
print(lineEditText)
I don't know how to take input of user in one textbox and show output in other textbox.
I tried but it's not working.
from PyQt5 import QtCore, QtGui, QtWidgets
import os
def click(self):
self.pushButton.click()
print("hello")
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(664, 389)
MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-image: url(:/background/White-Marble-Background-product-Photography-backdrops-Newborn-Photo-props-baby-shower-PhotoCall-pictures-newborn-propsXt-6735.jpg_640x640.webp);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 20, 621, 51))
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setObjectName("lineEdit")
inp_val = self.lineEdit.text()
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 290, 151, 41))
font = QtGui.QFont()
font.setFamily("Rubik")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(240, 184, 160);")
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(click(self))
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 90, 621, 191))
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.textEdit.setFont(font)
self.textEdit.setCursorWidth(2)
self.textEdit.setObjectName("textEdit")
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", "Rahul"))
self.pushButton.setText(_translate("MainWindow", "Run"))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Type Your Text Here....."))
self.textEdit.setPlaceholderText(_translate("MainWindow", "Your Output....."))
import test2_rc
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_())
How do I input in lineedit and show output in textedit?
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
# ? import os
#def click(self):
# self.pushButton.click()
# print("hello")
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(664, 389)
MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-image: url(:/background/White-Marble-Background-product-Photography-backdrops-Newborn-Photo-props-baby-shower-PhotoCall-pictures-newborn-propsXt-6735.jpg_640x640.webp);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 20, 621, 51))
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setObjectName("lineEdit")
inp_val = self.lineEdit.text()
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 290, 151, 41))
font = QtGui.QFont()
font.setFamily("Rubik")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(240, 184, 160);")
self.pushButton.setObjectName("pushButton")
# self.pushButton.clicked.connect(click(self))
self.pushButton.clicked.connect(self.onClick)
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 90, 621, 191))
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.textEdit.setFont(font)
self.textEdit.setCursorWidth(2)
self.textEdit.setObjectName("textEdit")
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 onClick(self):
# self.pushButton.click()
print("hello")
self.textEdit.append(self.lineEdit.text()) # <---
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Rahul"))
self.pushButton.setText(_translate("MainWindow", "Run"))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Type Your Text Here....."))
self.textEdit.setPlaceholderText(_translate("MainWindow", "Your Output....."))
# ? import test2_rc
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_())
Use setPlainText:
def click(self):
inputtext = self.lineEdit.text()
self.textEdit.setPlainText(inputtext)
Also you need to put function click inside class Ui_MainWindow, like this:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#your code
def click(self):
inputtext = self.lineEdit.text()
self.textEdit.setPlainText(inputtext)
Also call function click like:
self.pushButton.clicked.connect(self.click)
I am designing a GUI to capture an image from picamera, I am using PyQt5 to develop the GUI. I used camera.start_preview() function to see the live preview. After that I used a button named "Take" to capture the image and after capturing the image I showed the image in Qlabel by using self.label_2.setPixmap(QtGui.QPixmap()) command till this my code is working fine. Now I want to set the live preview of PiCamera into the same Qlabel, which I am using to show the captured image. Is it possible to set the live preview inside a QLabel?
After googling I found PIL can be used to stream live preview inside an window, but I have no idea how to implement it.
from PyQt5 import QtCore, QtGui, QtWidgets
from picamera import PiCamera
import time
from time import sleep
import gpiozero
from gpiozero import Button
from PIL import Image
import cups
import os
camera = PiCamera()
camera.sensor_mode = 2
camera.brightness = 70
camera.contrast = 20
photoHeight = 1944
photoWidth = 2592
class clsPicCount(object):
countpic=0
def __init__(self):
clsPicCount.countpic=clsPicCount.countpic+1
if clsPicCount.countpic > 1:
clsPicCount.countpic = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1366, 768)
MainWindow.setMinimumSize(QtCore.QSize(1366, 768))
MainWindow.setMaximumSize(QtCore.QSize(1366, 768))
font = QtGui.QFont()
font.setUnderline(False)
font.setStrikeOut(False)
MainWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("Photo.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.retake = QtWidgets.QPushButton(self.centralwidget)
self.retake.setGeometry(QtCore.QRect(320, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.retake.setFont(font)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("retake.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.retake.setIcon(icon1)
self.retake.setIconSize(QtCore.QSize(50, 50))
self.retake.setObjectName("retake")
self.take = QtWidgets.QPushButton(self.centralwidget)
self.take.setGeometry(QtCore.QRect(550, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.take.setFont(font)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("take.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.take.setIcon(icon2)
self.take.setIconSize(QtCore.QSize(60, 60))
self.take.setObjectName("take")
self.print = QtWidgets.QPushButton(self.centralwidget)
self.print.setGeometry(QtCore.QRect(780, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.print.setFont(font)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("print.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.print.setIcon(icon3)
self.print.setIconSize(QtCore.QSize(50, 50))
self.print.setCheckable(True)
self.print.setObjectName("print")
self.settings = QtWidgets.QPushButton(self.centralwidget)
self.settings.setGeometry(QtCore.QRect(1220, 690, 121, 51))
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
self.settings.setFont(font)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("settings.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.settings.setIcon(icon4)
self.settings.setIconSize(QtCore.QSize(30, 30))
self.settings.setObjectName("settings")
self.heading = QtWidgets.QLabel(self.centralwidget)
self.heading.setGeometry(QtCore.QRect(390, 20, 581, 31))
self.heading.setObjectName("heading")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(230, 70, 900, 600))
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap("photobooth.jpg"))
self.label_2.setScaledContents(True)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(0, 0, 1361, 771))
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap("background.png"))
self.label_3.setObjectName("label_3")
self.label_3.raise_()
self.retake.raise_()
self.take.raise_()
self.print.raise_()
self.settings.raise_()
self.heading.raise_()
self.label_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Defining functions of our buttons
self.take.clicked.connect(self.startPreview)
#Initializing Printer
conn = cups.Connection ()
printers = conn.getPrinters ()
printer_name = list(printers)[0]
for printer in printers:
print (printer, printers[printer]["device-uri"])
#Code Block for live preview and image capture
def startPreview(self):
p=clsPicCount()
print(clsPicCount.countpic)
if clsPicCount.countpic == 1:
camera.start_preview(fullscreen=False, window=(10,0,320,240))
else:
filename = time.strftime("%Y%m%d-%H%M%S")
print (filename)
camera.capture('/home/pi/Desktop/%s.jpg'%filename, resize = (photoWidth, photoHeight))
camera.stop_preview()
self.label_2.setPixmap(QtGui.QPixmap('/home/pi/Desktop/%s.jpg'%filename))
self.label_2.setScaledContents(True)
#self.label_3.raise_()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "PhotoBooth"))
self.retake.setText(_translate("MainWindow", "RETAKE"))
self.take.setText(_translate("MainWindow", "TAKE"))
self.print.setText(_translate("MainWindow", "PRINT"))
self.settings.setText(_translate("MainWindow", "SETTINGS"))
self.heading.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:14pt; font-weight:600; color:#ffffff;\">TECH MECH SERVICES PVT LTD.</span></p></body></html>"))
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_())