deleting a text from QtextEdit after pressing a pushbutton Pyqt5 - python

this a chatbot using chatterbot i m trying to delete the text that the user writes in textEdit once he clicks a pushbutton or presses Enter like tkinter you can do like: msgs.insert(END, "You : " + query) and it deletes the text once the button is pressed
from PyQt5 import QtCore, QtGui, QtWidgets
from chatterbot import ChatBot
bot = ChatBot('butty bot')
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
bot = ChatBot('butty bot')
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(402, 659)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
def action():
query = self.textEdit.toPlainText()
response = bot.get_response(query)
self.listbox.addItem("You : " + query)
self.listbox.addItem("Butty bot : " + str(response))
self.send_btn = QtWidgets.QPushButton(self.centralwidget)
self.send_btn.setGeometry(QtCore.QRect(140, 550, 111, 41))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.send_btn.setFont(font)
self.send_btn.setObjectName("send_btn")
self.send_btn.clicked.connect(action)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 10, 121, 21))
font = QtGui.QFont()
font.setFamily("Lucida Fax")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 510, 381, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")

Related

AttributeError: 'Ui_Dialog' object has no attribute 'textEdit_2'

I am having trouble setting the inheritance. I want to activate the generator function when the pushButton_3 is clicked but I keep getting the error in the title. My full code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow
import pyqrcode
import png
from pyqrcode import QRCode
class MainWindow(QMainWindow):
def setupUi(self):
self.setObjectName("QR Generator")
self.setFixedSize(591, 381)
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(340, 320, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self)
self.pushButton_2.setGeometry(QtCore.QRect(340, 260, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(10, 200, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.generate())
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(10, 260, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.graphicsView = QtWidgets.QGraphicsView(self)
self.graphicsView.setGeometry(QtCore.QRect(340, 10, 241, 241))
self.graphicsView.setObjectName("graphicsView")
self.textEdit = QtWidgets.QTextEdit(self)
self.textEdit.setGeometry(QtCore.QRect(10, 150, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(40, 20, 261, 61))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit_2 = QtWidgets.QTextEdit(self)
self.textEdit_2.setGeometry(QtCore.QRect(10, 100, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit_2.setFont(font)
self.textEdit_2.setObjectName("textEdit_2")
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Dialog", "QR Code Generator"))
self.pushButton.setText(_translate("Dialog", "Exit"))
self.pushButton_2.setText(_translate("Dialog", "Save As Picture"))
self.pushButton_3.setText(_translate("Dialog", "Generate QR Code"))
self.pushButton_4.setText(_translate("Dialog", "Clear"))
self.textEdit.setPlaceholderText(_translate("Dialog", "Paste link here"))
self.label.setText(_translate("Dialog", "QR Code Generator"))
self.textEdit_2.setPlaceholderText(_translate("Dialog", "Give a name to the QR"))
def save1(self):
pass
def generate(self):
link = self.textEdit_2.text()
qrname = self.textEdit.text()
qrcode = pyqrcode.create(link)
qrcode.png(f"{qrname}.png", scale = 6)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
self = QtWidgets.QDialog()
ui = MainWindow()
ui.setupUi()
self.show()
sys.exit(app.exec_())
self.pushButton_3.clicked.connect(self.generate()) at this line you are not connecting the generate function but you are calling it by adding () to function name, so change it to self.pushButton_3.clicked.connect(self.generate) and self.generate should accept one argument x which is callback event of pushbutton
so either change definition of self.generate which accepts one argument
or make a lambda function which accepts one argument and calls self.generate by placing this line self.pushButton_3.clicked.connect(lambda x: self.generate())

how to print continous data on pyqt5 text browser

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_())

Cannot open second window, Event loop already running - PyQt5 [duplicate]

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.

How do I pass parameters from the GUI PyQt5 widgets to the python code I have working through a button

This is something I'm doing for work, to speed up a process with the software we use. I'm trying to change a text file by using different regex search & replace functions. I have that working, and I have the GUI the way I want it.
import MainCode
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(786, 73)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(786, 73))
MainWindow.setWindowOpacity(1.0)
MainWindow.setDockNestingEnabled(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.StyleGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.StyleGroupBox.setGeometry(QtCore.QRect(0, 10, 101, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.StyleGroupBox.setFont(font)
self.StyleGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.StyleGroupBox.setCheckable(True)
self.StyleGroupBox.setChecked(False)
self.StyleGroupBox.setObjectName("StyleGroupBox")
self.StyleComboBox = QtWidgets.QComboBox(self.StyleGroupBox)
self.StyleComboBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
self.StyleComboBox.setMinimumSize(QtCore.QSize(69, 0))
self.StyleComboBox.setSizeIncrement(QtCore.QSize(5, 5))
self.StyleComboBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.StyleComboBox.setMaxCount(100)
self.StyleComboBox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
self.StyleComboBox.setMinimumContentsLength(1)
self.StyleComboBox.setDuplicatesEnabled(True)
self.StyleComboBox.setObjectName("StyleComboBox")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.FontGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.FontGroupBox.setEnabled(True)
self.FontGroupBox.setGeometry(QtCore.QRect(100, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.FontGroupBox.setFont(font)
self.FontGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.FontGroupBox.setCheckable(True)
self.FontGroupBox.setChecked(False)
self.FontGroupBox.setObjectName("FontGroupBox")
self.FontSpinBox = QtWidgets.QSpinBox(self.FontGroupBox)
self.FontSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.FontSpinBox.setFont(font)
self.FontSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.FontSpinBox.setMinimum(8)
self.FontSpinBox.setMaximum(200)
self.FontSpinBox.setProperty("value", 24)
self.FontSpinBox.setObjectName("FontSpinBox")
self.OpacityGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.OpacityGroupBox.setEnabled(True)
self.OpacityGroupBox.setGeometry(QtCore.QRect(180, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.OpacityGroupBox.setFont(font)
self.OpacityGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.OpacityGroupBox.setCheckable(True)
self.OpacityGroupBox.setChecked(False)
self.OpacityGroupBox.setObjectName("OpacityGroupBox")
self.OpacityDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.OpacityGroupBox)
self.OpacityDoubleSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.OpacityDoubleSpinBox.setFont(font)
self.OpacityDoubleSpinBox.setToolTip("")
self.OpacityDoubleSpinBox.setDecimals(2)
self.OpacityDoubleSpinBox.setMaximum(1.0)
self.OpacityDoubleSpinBox.setSingleStep(0.01)
self.OpacityDoubleSpinBox.setProperty("value", 1.0)
self.OpacityDoubleSpinBox.setObjectName("OpacityDoubleSpinBox")
self.NextPageGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.NextPageGroupBox.setEnabled(True)
self.NextPageGroupBox.setGeometry(QtCore.QRect(260, 10, 91, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.NextPageGroupBox.setFont(font)
self.NextPageGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NextPageGroupBox.setCheckable(True)
self.NextPageGroupBox.setChecked(False)
self.NextPageGroupBox.setObjectName("NextPageGroupBox")
self.NextPageSpinBox = QtWidgets.QSpinBox(self.NextPageGroupBox)
self.NextPageSpinBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.NextPageSpinBox.setFont(font)
self.NextPageSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.NextPageSpinBox.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.NextPageSpinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.PlusMinus)
self.NextPageSpinBox.setAccelerated(True)
self.NextPageSpinBox.setMaximum(999999999)
self.NextPageSpinBox.setDisplayIntegerBase(10)
self.NextPageSpinBox.setObjectName("NextPageSpinBox")
self.ZindexGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.ZindexGroupBox.setEnabled(True)
self.ZindexGroupBox.setGeometry(QtCore.QRect(350, 10, 71, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.ZindexGroupBox.setFont(font)
self.ZindexGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.ZindexGroupBox.setCheckable(True)
self.ZindexGroupBox.setChecked(False)
self.ZindexGroupBox.setObjectName("ZindexGroupBox")
self.ZindexSpinBox = QtWidgets.QSpinBox(self.ZindexGroupBox)
self.ZindexSpinBox.setGeometry(QtCore.QRect(10, 20, 51, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.ZindexSpinBox.setFont(font)
self.ZindexSpinBox.setObjectName("ZindexSpinBox")
self.AddChangeFrame = QtWidgets.QFrame(self.centralwidget)
self.AddChangeFrame.setEnabled(True)
self.AddChangeFrame.setGeometry(QtCore.QRect(420, 0, 351, 71))
font = QtGui.QFont()
font.setPointSize(8)
self.AddChangeFrame.setFont(font)
self.AddChangeFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.AddChangeFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.AddChangeFrame.setObjectName("AddChangeFrame")
self.NewButtonsSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.NewButtonsSpinBox.setGeometry(QtCore.QRect(10, 20, 41, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.NewButtonsSpinBox.setFont(font)
self.NewButtonsSpinBox.setMouseTracking(True)
self.NewButtonsSpinBox.setAlignment(QtCore.Qt.AlignCenter)
self.NewButtonsSpinBox.setMinimum(1)
self.NewButtonsSpinBox.setObjectName("NewButtonsSpinBox")
self.NewButtonsRadio = QtWidgets.QRadioButton(self.AddChangeFrame)
self.NewButtonsRadio.setGeometry(QtCore.QRect(0, 0, 161, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.NewButtonsRadio.setFont(font)
self.NewButtonsRadio.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NewButtonsRadio.setObjectName("NewButtonsRadio")
self.StartItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.StartItemLabel.setGeometry(QtCore.QRect(70, 20, 61, 21))
self.StartItemLabel.setObjectName("StartItemLabel")
self.EndingItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.EndingItemLabel.setGeometry(QtCore.QRect(160, 20, 51, 20))
self.EndingItemLabel.setObjectName("EndingItemLabel")
self.StartItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.StartItemNumberSpinBox.setGeometry(QtCore.QRect(60, 40, 81, 22))
self.StartItemNumberSpinBox.setObjectName("StartItemNumberSpinBox")
self.EndItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.EndItemNumberSpinBox.setGeometry(QtCore.QRect(150, 40, 81, 22))
self.EndItemNumberSpinBox.setObjectName("EndItemNumberSpinBox")
self.ChangeRadioButton = QtWidgets.QRadioButton(self.AddChangeFrame)
self.ChangeRadioButton.setGeometry(QtCore.QRect(170, 0, 181, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.ChangeRadioButton.setFont(font)
self.ChangeRadioButton.setChecked(True)
self.ChangeRadioButton.setObjectName("ChangeRadioButton")
self.SubmitChangesButton = QtWidgets.QPushButton(self.centralwidget)
self.SubmitChangesButton.setGeometry(QtCore.QRect(660, 30, 131, 41))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(11)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
font.setKerning(True)
self.SubmitChangesButton.setFont(font)
self.SubmitChangesButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.SubmitChangesButton.setAutoFillBackground(True)
self.SubmitChangesButton.setObjectName("SubmitChangesButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simphony Button Editor"))
self.StyleGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the style & color of the button using the drop-down menu.</p></body></html>"))
self.StyleGroupBox.setWhatsThis(_translate("MainWindow", "<html><head/><body><p>Changes style & color of button</p></body></html>"))
self.StyleGroupBox.setTitle(_translate("MainWindow", "Color/ Style"))
self.StyleComboBox.setItemText(0, _translate("MainWindow", "Button1"))
self.StyleComboBox.setItemText(1, _translate("MainWindow", "Button2"))
self.StyleComboBox.setItemText(2, _translate("MainWindow", "Button3"))
self.StyleComboBox.setItemText(3, _translate("MainWindow", "Button4"))
self.StyleComboBox.setItemText(4, _translate("MainWindow", "Button5"))
self.FontGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the font size the button text.</p></body></html>"))
self.FontGroupBox.setTitle(_translate("MainWindow", "Font Size"))
self.OpacityGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the percentage Opacity (visibility) of the button.</p></body></html>"))
self.OpacityGroupBox.setTitle(_translate("MainWindow", "Opacity"))
self.NextPageGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Assigns the next menu page to jump to after button completion. </p></body></html>"))
self.NextPageGroupBox.setTitle(_translate("MainWindow", "Next Page"))
self.ZindexGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the visibility layer of the button. If a button is set to 1 and another set to 2 in the same spot then you would only see the button with Z index 2.</p></body></html>"))
self.ZindexGroupBox.setTitle(_translate("MainWindow", "Z Index"))
self.NewButtonsRadio.setText(_translate("MainWindow", "Create New Buttons"))
self.StartItemLabel.setText(_translate("MainWindow", "Start Item#"))
self.EndingItemLabel.setText(_translate("MainWindow", "End Item#"))
self.StartItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Enter Item number. Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.EndItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.ChangeRadioButton.setText(_translate("MainWindow", "Update Copied Buttons"))
self.SubmitChangesButton.setText(_translate("MainWindow", "Make Changes"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.SubmitChangesButton.clicked.connect(self.on_clicked)
#QtCore.pyqtSlot()
def on_clicked(self):
if self.FontGroupBox.isChecked() is True:
print('Font Size = ' + self.FontSpinBox.value())
if self.StyleGroupBox.isChecked() is True:
print(self.StyleComboBox.Text())
if self.OpacityGroupBox.isChecked() is True:
print('Opacity% = ' + self.OpacityDoubleSpinBox.value())
if self.NextPageGroupBox.isChecked() is True:
print('Next Page = ' + self.NextPageSpinBox.value())
if self.ZindexGroupBox.isChecked() is True:
print('Z Index = ' + self.ZindexSpinBox.value())
if __name__ == "__main__":
import sys
import MainCode
import MyDesign
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindo`enter code here`w()
ui.setupUi(MainWindow)
MainWindow.show()
print(Ui_MainWindow)
sys.exit(app.exec_())
I just need to be able to pull the results from the input that has been entered, if the frames are enabled(checked) when I click the submit buttons. Output should be true or false for checkboxes and radio buttons and integers, a double, and a string for the rest. I'm sure I'm missing something simple.
We created a class MainWindow for the top-level window.
Create an instance of it and display the widget on the screen.
w = MainWindow()
w.show()
Added the ability to get the selected item in the QComboBox drop-down list.
Corrected the output format in the on_clicked method.
#import MainCode
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(786, 73)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(786, 73))
MainWindow.setWindowOpacity(1.0)
MainWindow.setDockNestingEnabled(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.StyleGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.StyleGroupBox.setGeometry(QtCore.QRect(0, 10, 101, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.StyleGroupBox.setFont(font)
self.StyleGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.StyleGroupBox.setCheckable(True)
self.StyleGroupBox.setChecked(False)
self.StyleGroupBox.setObjectName("StyleGroupBox")
self.StyleComboBox = QtWidgets.QComboBox(self.StyleGroupBox)
self.StyleComboBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
self.StyleComboBox.setMinimumSize(QtCore.QSize(69, 0))
self.StyleComboBox.setSizeIncrement(QtCore.QSize(5, 5))
self.StyleComboBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.StyleComboBox.setMaxCount(100)
self.StyleComboBox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
self.StyleComboBox.setMinimumContentsLength(1)
self.StyleComboBox.setDuplicatesEnabled(True)
self.StyleComboBox.setObjectName("StyleComboBox")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.FontGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.FontGroupBox.setEnabled(True)
self.FontGroupBox.setGeometry(QtCore.QRect(100, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.FontGroupBox.setFont(font)
self.FontGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.FontGroupBox.setCheckable(True)
self.FontGroupBox.setChecked(False)
self.FontGroupBox.setObjectName("FontGroupBox")
self.FontSpinBox = QtWidgets.QSpinBox(self.FontGroupBox)
self.FontSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.FontSpinBox.setFont(font)
self.FontSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.FontSpinBox.setMinimum(8)
self.FontSpinBox.setMaximum(200)
self.FontSpinBox.setProperty("value", 24)
self.FontSpinBox.setObjectName("FontSpinBox")
self.OpacityGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.OpacityGroupBox.setEnabled(True)
self.OpacityGroupBox.setGeometry(QtCore.QRect(180, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.OpacityGroupBox.setFont(font)
self.OpacityGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.OpacityGroupBox.setCheckable(True)
self.OpacityGroupBox.setChecked(False)
self.OpacityGroupBox.setObjectName("OpacityGroupBox")
self.OpacityDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.OpacityGroupBox)
self.OpacityDoubleSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.OpacityDoubleSpinBox.setFont(font)
self.OpacityDoubleSpinBox.setToolTip("")
self.OpacityDoubleSpinBox.setDecimals(2)
self.OpacityDoubleSpinBox.setMaximum(1.0)
self.OpacityDoubleSpinBox.setSingleStep(0.01)
self.OpacityDoubleSpinBox.setProperty("value", 1.0)
self.OpacityDoubleSpinBox.setObjectName("OpacityDoubleSpinBox")
self.NextPageGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.NextPageGroupBox.setEnabled(True)
self.NextPageGroupBox.setGeometry(QtCore.QRect(260, 10, 91, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.NextPageGroupBox.setFont(font)
self.NextPageGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NextPageGroupBox.setCheckable(True)
self.NextPageGroupBox.setChecked(False)
self.NextPageGroupBox.setObjectName("NextPageGroupBox")
self.NextPageSpinBox = QtWidgets.QSpinBox(self.NextPageGroupBox)
self.NextPageSpinBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.NextPageSpinBox.setFont(font)
self.NextPageSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.NextPageSpinBox.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.NextPageSpinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.PlusMinus)
self.NextPageSpinBox.setAccelerated(True)
self.NextPageSpinBox.setMaximum(999999999)
self.NextPageSpinBox.setDisplayIntegerBase(10)
self.NextPageSpinBox.setObjectName("NextPageSpinBox")
self.ZindexGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.ZindexGroupBox.setEnabled(True)
self.ZindexGroupBox.setGeometry(QtCore.QRect(350, 10, 71, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.ZindexGroupBox.setFont(font)
self.ZindexGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.ZindexGroupBox.setCheckable(True)
self.ZindexGroupBox.setChecked(False)
self.ZindexGroupBox.setObjectName("ZindexGroupBox")
self.ZindexSpinBox = QtWidgets.QSpinBox(self.ZindexGroupBox)
self.ZindexSpinBox.setGeometry(QtCore.QRect(10, 20, 51, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.ZindexSpinBox.setFont(font)
self.ZindexSpinBox.setObjectName("ZindexSpinBox")
self.AddChangeFrame = QtWidgets.QFrame(self.centralwidget)
self.AddChangeFrame.setEnabled(True)
self.AddChangeFrame.setGeometry(QtCore.QRect(420, 0, 351, 71))
font = QtGui.QFont()
font.setPointSize(8)
self.AddChangeFrame.setFont(font)
self.AddChangeFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.AddChangeFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.AddChangeFrame.setObjectName("AddChangeFrame")
self.NewButtonsSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.NewButtonsSpinBox.setGeometry(QtCore.QRect(10, 20, 41, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.NewButtonsSpinBox.setFont(font)
self.NewButtonsSpinBox.setMouseTracking(True)
self.NewButtonsSpinBox.setAlignment(QtCore.Qt.AlignCenter)
self.NewButtonsSpinBox.setMinimum(1)
self.NewButtonsSpinBox.setObjectName("NewButtonsSpinBox")
self.NewButtonsRadio = QtWidgets.QRadioButton(self.AddChangeFrame)
self.NewButtonsRadio.setGeometry(QtCore.QRect(0, 0, 161, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.NewButtonsRadio.setFont(font)
self.NewButtonsRadio.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NewButtonsRadio.setObjectName("NewButtonsRadio")
self.StartItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.StartItemLabel.setGeometry(QtCore.QRect(70, 20, 61, 21))
self.StartItemLabel.setObjectName("StartItemLabel")
self.EndingItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.EndingItemLabel.setGeometry(QtCore.QRect(160, 20, 51, 20))
self.EndingItemLabel.setObjectName("EndingItemLabel")
self.StartItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.StartItemNumberSpinBox.setGeometry(QtCore.QRect(60, 40, 81, 22))
self.StartItemNumberSpinBox.setObjectName("StartItemNumberSpinBox")
self.EndItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.EndItemNumberSpinBox.setGeometry(QtCore.QRect(150, 40, 81, 22))
self.EndItemNumberSpinBox.setObjectName("EndItemNumberSpinBox")
self.ChangeRadioButton = QtWidgets.QRadioButton(self.AddChangeFrame)
self.ChangeRadioButton.setGeometry(QtCore.QRect(170, 0, 181, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.ChangeRadioButton.setFont(font)
self.ChangeRadioButton.setChecked(True)
self.ChangeRadioButton.setObjectName("ChangeRadioButton")
self.SubmitChangesButton = QtWidgets.QPushButton(self.centralwidget)
self.SubmitChangesButton.setGeometry(QtCore.QRect(660, 30, 131, 41))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(11)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
font.setKerning(True)
self.SubmitChangesButton.setFont(font)
self.SubmitChangesButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.SubmitChangesButton.setAutoFillBackground(True)
self.SubmitChangesButton.setObjectName("SubmitChangesButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simphony Button Editor"))
self.StyleGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the style & color of the button using the drop-down menu.</p></body></html>"))
self.StyleGroupBox.setWhatsThis(_translate("MainWindow", "<html><head/><body><p>Changes style & color of button</p></body></html>"))
self.StyleGroupBox.setTitle(_translate("MainWindow", "Color/ Style"))
self.StyleComboBox.setItemText(0, _translate("MainWindow", "Button1"))
self.StyleComboBox.setItemText(1, _translate("MainWindow", "Button2"))
self.StyleComboBox.setItemText(2, _translate("MainWindow", "Button3"))
self.StyleComboBox.setItemText(3, _translate("MainWindow", "Button4"))
self.StyleComboBox.setItemText(4, _translate("MainWindow", "Button5"))
self.FontGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the font size the button text.</p></body></html>"))
self.FontGroupBox.setTitle(_translate("MainWindow", "Font Size"))
self.OpacityGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the percentage Opacity (visibility) of the button.</p></body></html>"))
self.OpacityGroupBox.setTitle(_translate("MainWindow", "Opacity"))
self.NextPageGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Assigns the next menu page to jump to after button completion. </p></body></html>"))
self.NextPageGroupBox.setTitle(_translate("MainWindow", "Next Page"))
self.ZindexGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the visibility layer of the button. If a button is set to 1 and another set to 2 in the same spot then you would only see the button with Z index 2.</p></body></html>"))
self.ZindexGroupBox.setTitle(_translate("MainWindow", "Z Index"))
self.NewButtonsRadio.setText(_translate("MainWindow", "Create New Buttons"))
self.StartItemLabel.setText(_translate("MainWindow", "Start Item#"))
self.EndingItemLabel.setText(_translate("MainWindow", "End Item#"))
self.StartItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Enter Item number. Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.EndItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.ChangeRadioButton.setText(_translate("MainWindow", "Update Copied Buttons"))
self.SubmitChangesButton.setText(_translate("MainWindow", "Make Changes"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.SubmitChangesButton.clicked.connect(self.on_clicked)
# + vvvv
self.itemTextComboBox = "Button1"
self.StyleComboBox.activated[str].connect(self.activatedItemComboBox)
def activatedItemComboBox(self, text):
self.itemTextComboBox = text
# + ^^^^
#QtCore.pyqtSlot()
def on_clicked(self):
if self.StyleGroupBox.isChecked() is True:
print(self.itemTextComboBox)
if self.FontGroupBox.isChecked() is True:
print('Font Size = {}'.format(self.FontSpinBox.value()))
if self.OpacityGroupBox.isChecked() is True:
print('Opacity% = {:.2f}'.format(self.OpacityDoubleSpinBox.value()))
if self.NextPageGroupBox.isChecked() is True:
print('Next Page = {}'.format(self.NextPageSpinBox.value()))
if self.ZindexGroupBox.isChecked() is True:
print('Z Index = {}'.format(self.ZindexSpinBox.value()))
if __name__ == "__main__":
import sys
# import MainCode
# import MyDesign
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
# print(Ui_MainWindow)
w = MainWindow() # <---
w.show() # <---
sys.exit(app.exec_())

How to show the live preview from picamera in a GUI Window made by PyQt5?

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_())

Categories