Python PyQt5 label into global variable - python

I have Ui_MainWindow class, in this class there is a function setupUI and sendmail function.
In setupUI there are lineEdit and lineEdit_2. I have to put text of this variables into the another variables in sendmail def and use it in setupUI How can I do this ?
This code must send text of that line edits to my gmail.
This is code of sendmail func:
class Ui_MainWindow(object):
myaddr = "my#gmail.com"
mypass = "my_password"
toaddr = "your#gmail.com"
def sendmail(label_2, label_3):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(myaddr, mypass)
server.sendmail(myaddr, toaddr, message)
server.quit()
And this is setupUI code:
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(470, 332)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.warface = QtWidgets.QLabel(self.centralwidget)
self.warface.setGeometry(QtCore.QRect(160, 0, 171, 41))
font = QtGui.QFont()
font.setPointSize(22)
self.warface.setFont(font)
self.warface.setObjectName("warface")
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(330, 80, 101, 17))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(330, 110, 101, 17))
self.radioButton_2.setObjectName("radioButton_2")
self.radioButton_3 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_3.setGeometry(QtCore.QRect(330, 140, 101, 17))
self.radioButton_3.setObjectName("radioButton_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 200, 141, 51))
font = QtGui.QFont()
font.setPointSize(9)
self.pushButton.setFont(font)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 80, 51, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 120, 51, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(80, 130, 151, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(80, 90, 151, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 470, 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)
self.pushButton.clicked.connect(self.sendmail)

Look at the QLabel.text() function. You can get the text of a label by using
self.label_2.text() and store it in a variable there after myaddr, mypass, toaddr.
Or simply define a variable and give it the self object from setupUI() so you can access its QLabels.

Related

QT Designer - UI for Python

I have designed a UI in QT Designer and successfully converted into Python code as well. It is executing fine in PyCharm window.
However, I want to capture the inputs in each widget into a file and am stuck without any luck.
# Form implementation generated from reading ui file 'Pre_Sales_1.ui'
#
# Created by: PyQt6 UI code generator 6.2.3
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1116, 718)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame1 = QtWidgets.QFrame(self.centralwidget)
self.frame1.setGeometry(QtCore.QRect(10, 20, 361, 161))
self.frame1.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame1.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame1.setObjectName("frame1")
self.label_2 = QtWidgets.QLabel(self.frame1)
self.label_2.setGeometry(QtCore.QRect(0, 10, 131, 31))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.frame1)
self.lineEdit.setGeometry(QtCore.QRect(140, 10, 221, 22))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setPlaceholderText("Please Enter the name of the Customer")
self.label_3 = QtWidgets.QLabel(self.frame1)
self.label_3.setGeometry(QtCore.QRect(0, 50, 131, 31))
self.label_3.setObjectName("label_3")
self.textEdit = QtWidgets.QTextEdit(self.frame1)
self.textEdit.setGeometry(QtCore.QRect(140, 40, 221, 111))
self.textEdit.setObjectName("textEdit")
self.textEdit.setPlaceholderText("Please Enter the Primary Address of the Customer")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(500, 0, 231, 16))
self.label.setObjectName("label")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(390, 20, 371, 161))
self.frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame.setObjectName("frame")
self.label_4 = QtWidgets.QLabel(self.frame)
self.label_4.setGeometry(QtCore.QRect(0, 10, 141, 20))
self.label_4.setObjectName("label_4")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 10, 221, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 50, 221, 22))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_4.setGeometry(QtCore.QRect(140, 130, 221, 22))
self.lineEdit_4.setObjectName("lineEdit_4")
self.label_5 = QtWidgets.QLabel(self.frame)
self.label_5.setGeometry(QtCore.QRect(0, 50, 141, 20))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.frame)
self.label_6.setGeometry(QtCore.QRect(0, 90, 141, 20))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.frame)
self.label_7.setGeometry(QtCore.QRect(0, 130, 141, 20))
self.label_7.setObjectName("label_7")
self.lineEdit_5 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_5.setGeometry(QtCore.QRect(140, 90, 221, 22))
self.lineEdit_5.setObjectName("lineEdit_5")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(760, 20, 351, 151))
self.calendarWidget.setObjectName("calendarWidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 190, 1111, 441))
self.tabWidget.setAutoFillBackground(False)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.frame_2 = QtWidgets.QFrame(self.tab)
self.frame_2.setGeometry(QtCore.QRect(0, 20, 201, 80))
self.frame_2.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_2.setObjectName("frame_2")
self.label_8 = QtWidgets.QLabel(self.frame_2)
self.label_8.setGeometry(QtCore.QRect(10, 0, 111, 16))
self.label_8.setObjectName("label_8")
self.checkBox = QtWidgets.QCheckBox(self.frame_2)
self.checkBox.setGeometry(QtCore.QRect(10, 30, 181, 20))
self.checkBox.setTristate(False)
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(self.frame_2)
self.checkBox_2.setGeometry(QtCore.QRect(10, 60, 181, 20))
self.checkBox_2.setObjectName("checkBox_2")
self.frame_3 = QtWidgets.QFrame(self.frame_2)
self.frame_3.setGeometry(QtCore.QRect(200, 50, 201, 80))
self.frame_3.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_3.setObjectName("frame_3")
self.label_9 = QtWidgets.QLabel(self.frame_3)
self.label_9.setGeometry(QtCore.QRect(10, 0, 111, 16))
self.label_9.setObjectName("label_9")
self.checkBox_3 = QtWidgets.QCheckBox(self.frame_3)
self.checkBox_3.setGeometry(QtCore.QRect(10, 20, 181, 20))
self.checkBox_3.setTristate(False)
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(self.frame_3)
self.checkBox_4.setGeometry(QtCore.QRect(10, 40, 181, 20))
self.checkBox_4.setObjectName("checkBox_4")
self.frame_4 = QtWidgets.QFrame(self.frame_2)
self.frame_4.setGeometry(QtCore.QRect(200, 40, 201, 80))
self.frame_4.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_4.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_4.setObjectName("frame_4")
self.label_10 = QtWidgets.QLabel(self.frame_4)
self.label_10.setGeometry(QtCore.QRect(10, 0, 111, 16))
self.label_10.setObjectName("label_10")
self.checkBox_5 = QtWidgets.QCheckBox(self.frame_4)
self.checkBox_5.setGeometry(QtCore.QRect(10, 20, 181, 20))
self.checkBox_5.setTristate(False)
self.checkBox_5.setObjectName("checkBox_5")
self.checkBox_6 = QtWidgets.QCheckBox(self.frame_4)
self.checkBox_6.setGeometry(QtCore.QRect(10, 40, 181, 20))
self.checkBox_6.setObjectName("checkBox_6")
self.frame_5 = QtWidgets.QFrame(self.frame_4)
self.frame_5.setGeometry(QtCore.QRect(200, 50, 201, 80))
self.frame_5.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_5.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_5.setObjectName("frame_5")
self.label_11 = QtWidgets.QLabel(self.frame_5)
self.label_11.setGeometry(QtCore.QRect(10, 0, 111, 16))
self.label_11.setObjectName("label_11")
self.checkBox_7 = QtWidgets.QCheckBox(self.frame_5)
self.checkBox_7.setGeometry(QtCore.QRect(10, 20, 181, 20))
self.checkBox_7.setTristate(False)
self.checkBox_7.setObjectName("checkBox_7")
self.checkBox_8 = QtWidgets.QCheckBox(self.frame_5)
self.checkBox_8.setGeometry(QtCore.QRect(10, 40, 181, 20))
self.checkBox_8.setObjectName("checkBox_8")
self.frame_6 = QtWidgets.QFrame(self.tab)
self.frame_6.setGeometry(QtCore.QRect(210, 20, 611, 91))
self.frame_6.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_6.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_6.setObjectName("frame_6")
self.label_12 = QtWidgets.QLabel(self.frame_6)
self.label_12.setGeometry(QtCore.QRect(270, 0, 101, 16))
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.frame_6)
self.label_13.setGeometry(QtCore.QRect(10, 20, 51, 21))
self.label_13.setObjectName("label_13")
self.label_14 = QtWidgets.QLabel(self.frame_6)
self.label_14.setGeometry(QtCore.QRect(0, 60, 61, 21))
self.label_14.setObjectName("label_14")
self.comboBox = QtWidgets.QComboBox(self.frame_6)
self.comboBox.setGeometry(QtCore.QRect(60, 20, 221, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox_2 = QtWidgets.QComboBox(self.frame_6)
self.comboBox_2.setGeometry(QtCore.QRect(60, 60, 221, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.label_15 = QtWidgets.QLabel(self.frame_6)
self.label_15.setGeometry(QtCore.QRect(290, 20, 101, 21))
self.label_15.setObjectName("label_15")
self.comboBox_3 = QtWidgets.QComboBox(self.frame_6)
self.comboBox_3.setGeometry(QtCore.QRect(390, 20, 221, 22))
self.comboBox_3.setObjectName("comboBox_3")
self.comboBox_4 = QtWidgets.QComboBox(self.frame_6)
self.comboBox_4.setGeometry(QtCore.QRect(390, 60, 221, 22))
self.comboBox_4.setObjectName("comboBox_4")
self.label_16 = QtWidgets.QLabel(self.frame_6)
self.label_16.setGeometry(QtCore.QRect(310, 60, 71, 21))
self.label_16.setObjectName("label_16")
self.frame_7 = QtWidgets.QFrame(self.frame_6)
self.frame_7.setGeometry(QtCore.QRect(0, 0, 611, 91))
self.frame_7.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_7.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_7.setObjectName("frame_7")
self.label_18 = QtWidgets.QLabel(self.frame_7)
self.label_18.setGeometry(QtCore.QRect(270, 0, 101, 16))
self.label_18.setObjectName("label_18")
self.label_19 = QtWidgets.QLabel(self.frame_7)
self.label_19.setGeometry(QtCore.QRect(10, 20, 51, 21))
self.label_19.setObjectName("label_19")
self.label_20 = QtWidgets.QLabel(self.frame_7)
self.label_20.setGeometry(QtCore.QRect(0, 60, 61, 21))
self.label_20.setObjectName("label_20")
self.comboBox_5 = QtWidgets.QComboBox(self.frame_7)
self.comboBox_5.setGeometry(QtCore.QRect(60, 20, 221, 22))
self.comboBox_5.setObjectName("comboBox_5")
self.comboBox_5.addItems(["Discrete Manufacturing","Chemicals"])
self.comboBox_6 = QtWidgets.QComboBox(self.frame_7)
self.comboBox_6.setGeometry(QtCore.QRect(60, 60, 221, 22))
self.comboBox_6.setObjectName("comboBox_6")
self.label_21 = QtWidgets.QLabel(self.frame_7)
self.label_21.setGeometry(QtCore.QRect(290, 20, 101, 21))
self.label_21.setObjectName("label_21")
self.comboBox_7 = QtWidgets.QComboBox(self.frame_7)
self.comboBox_7.setGeometry(QtCore.QRect(390, 20, 221, 22))
self.comboBox_7.setObjectName("comboBox_7")
self.comboBox_8 = QtWidgets.QComboBox(self.frame_7)
self.comboBox_8.setGeometry(QtCore.QRect(390, 60, 221, 22))
self.comboBox_8.setObjectName("comboBox_8")
self.label_22 = QtWidgets.QLabel(self.frame_7)
self.label_22.setGeometry(QtCore.QRect(310, 60, 71, 21))
self.label_22.setObjectName("label_22")
self.groupBox = QtWidgets.QGroupBox(self.tab)
self.groupBox.setGeometry(QtCore.QRect(0, 0, 1101, 111))
self.groupBox.setObjectName("groupBox")
self.frame_8 = QtWidgets.QFrame(self.groupBox)
self.frame_8.setGeometry(QtCore.QRect(830, 19, 271, 91))
self.frame_8.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
self.frame_8.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.frame_8.setObjectName("frame_8")
self.label_17 = QtWidgets.QLabel(self.frame_8)
self.label_17.setGeometry(QtCore.QRect(90, 0, 101, 16))
self.label_17.setObjectName("label_17")
self.textEdit_2 = QtWidgets.QTextEdit(self.frame_8)
self.textEdit_2.setGeometry(QtCore.QRect(0, 20, 271, 71))
self.textEdit_2.setObjectName("textEdit_2")
self.groupBox.raise_()
self.frame_2.raise_()
self.frame_6.raise_()
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.tabWidget.addTab(self.tab_3, "")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.tabWidget.addTab(self.tab_4, "")
self.tab_5 = QtWidgets.QWidget()
self.tab_5.setObjectName("tab_5")
self.tabWidget.addTab(self.tab_5, "")
self.tab_6 = QtWidgets.QWidget()
self.tab_6.setObjectName("tab_6")
self.tabWidget.addTab(self.tab_6, "")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(1000, 640, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1116, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to KOVI Pre-Sales"))
self.label_2.setText(_translate("MainWindow", "Name of the Customer"))
self.label_3.setText(_translate("MainWindow", "Primary Address "))
self.label.setText(_translate("MainWindow", "Please Enter Customer Information"))
self.label_4.setText(_translate("MainWindow", "Primay Contact Person"))
self.label_5.setText(_translate("MainWindow", "Primay Contact Number"))
self.label_6.setText(_translate("MainWindow", "Mobile Number"))
self.label_7.setText(_translate("MainWindow", "Work Email ID"))
self.label_8.setText(_translate("MainWindow", "Opportunity Details"))
self.checkBox.setText(_translate("MainWindow", "Net New Implementation"))
self.checkBox_2.setText(_translate("MainWindow", "New Plant Roll-Out"))
self.label_9.setText(_translate("MainWindow", "Opportunity Details"))
self.checkBox_3.setText(_translate("MainWindow", "Net New Implementation"))
self.checkBox_4.setText(_translate("MainWindow", "New Plant Roll-Out"))
self.label_10.setText(_translate("MainWindow", "Opportunity Details"))
self.checkBox_5.setText(_translate("MainWindow", "Net New Implementation"))
self.checkBox_6.setText(_translate("MainWindow", "New Plant Roll-Out"))
self.label_11.setText(_translate("MainWindow", "Opportunity Details"))
self.checkBox_7.setText(_translate("MainWindow", "Net New Implementation"))
self.checkBox_8.setText(_translate("MainWindow", "New Plant Roll-Out"))
self.label_12.setText(_translate("MainWindow", "Business Details"))
self.label_13.setText(_translate("MainWindow", "Industry"))
self.label_14.setText(_translate("MainWindow", "Geography"))
self.label_15.setText(_translate("MainWindow", "Annual Business"))
self.label_16.setText(_translate("MainWindow", "Existing ERP"))
self.label_18.setText(_translate("MainWindow", "Business Details"))
self.label_19.setText(_translate("MainWindow", "Industry"))
self.label_20.setText(_translate("MainWindow", "Geography"))
self.label_21.setText(_translate("MainWindow", "Annual Business"))
self.label_22.setText(_translate("MainWindow", "Existing ERP"))
self.groupBox.setTitle(_translate("MainWindow", "Basic Details about Business of the Customer"))
self.label_17.setText(_translate("MainWindow", "Other Details"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Implementation"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "OS/DB Migration"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "S/4HANA_Conversion"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("MainWindow", "DC2Cloud Migration"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_5), _translate("MainWindow", "Upgrade"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_6), _translate("MainWindow", "Application Maintenance Service"))
self.pushButton.setText(_translate("MainWindow", "Submit"))
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 to switch window to another class in pyqt5

I have two classes made with pyqt like this:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(409, 119)
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(20, 50, 61, 20))
self.label_2.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(90, 20, 221, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(90, 50, 221, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(320, 20, 75, 81))
self.pushButton.setObjectName("pushButton")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(20, 20, 61, 20))
self.label_3.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(20, 80, 61, 20))
self.label_4.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_4.setObjectName("label_4")
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(90, 80, 221, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label_2.setText(_translate("Dialog", "Password"))
self.pushButton.setText(_translate("Dialog", "login"))
self.label_3.setText(_translate("Dialog", "Email"))
self.label_4.setText(_translate("Dialog", "distinct num"))
class Ui_Dialog2(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(485, 429)
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(10, 30, 461, 281))
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 10, 121, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(20, 330, 211, 16))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(10, 350, 391, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(410, 350, 61, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(10, 390, 461, 23))
self.pushButton_2.setObjectName("pushButton_2")
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", "process"))
self.label_2.setText(_translate("Dialog", "file"))
self.pushButton.setText(_translate("Dialog", "select"))
self.pushButton_2.setText(_translate("Dialog", "push"))
These two classes have different window sizes and many different things.
How can I switch between two windows based on the login button of Ui_Dialog?
I know how to trigger on the login button but I don't know how to switch to another class.
I want to move to individual dialogs rather than being done in one dialog.

Code defined in func not working but runs in main setupui func (pyqt5)

I am making a YouTube downloader for which I created a function which will run when user press download button but this works when the code is in the setupUi function (main function created by pyqt5). The problem is that it does not work when defined inside another function. I do not know why this is happening. The following is the code which I want to run.
def group(self):
if i == 0:
self.groupBox = QtWidgets.QGroupBox(self.scroll)
self.groupBox.setGeometry(QtCore.QRect(70, 20, 431, 91))
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(30, 10, 371, 21))
self.label.setObjectName("label")
self.label.setText("b")
self.progressBar = QtWidgets.QProgressBar(self.groupBox)
self.progressBar.setGeometry(QtCore.QRect(40, 50, 221, 20))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
The following the code of main setupUi function
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(662, 423)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.b1 = QtWidgets.QPushButton(self.centralwidget)
self.b1.setGeometry(QtCore.QRect(70, 20, 81, 31))
self.b1.clicked.connect(self.func)
self.b1.setPalette(palette)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.b1.setFont(font)
self.b1.setStyleSheet("background-color: rgb(0, 255, 0);")
self.b1.setObjectName("b1")
self.subwin = QtWidgets.QScrollArea(self.centralwidget)
self.subwin.setGeometry(QtCore.QRect(70, 70, 531, 251))
self.subwin.setStyleSheet("background-image:")
self.subwin.setWidgetResizable(True)
self.subwin.setObjectName("subwin")
self.scroll = QtWidgets.QWidget()
self.scroll.setGeometry(QtCore.QRect(0, 0, 529, 249))
self.scroll.setObjectName("scrollAreaWidgetContents")
self.subwin.setWidget(self.scroll)
self.input = QtWidgets.QLineEdit(self.centralwidget)
self.input.setGeometry(QtCore.QRect(250, 20, 351, 31))
self.input.setObjectName("input")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
I hope this code will be enough and relevant.

How to append serial data on textbrowser pyqt5 receiving from serial port python pyqt [duplicate]

This question already has answers here:
Connect to serial from a PyQt GUI
(2 answers)
Closed 2 years ago.
My goal is to create a GUI using PyQt5 with functionality to receive data from serial port.
This data should be printed into text browser inside the GUI.
Data should be printed there continuously.
Reading should be toggled by pushing button.
This is the GUI part of the code:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(540, 60, 101, 61))
font = QtGui.QFont()
font.setPointSize(12)
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(30, -10, 221, 71))
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, 180, 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(700, 240, 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_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(30, 240, 651, 71))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setObjectName("lineEdit_2")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(260, 150, 381, 51))
font = QtGui.QFont()
font.setPointSize(12)
self.textBrowser.setFont(font)
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(30, 371, 731, 181))
font = QtGui.QFont()
font.setPointSize(12)
self.textBrowser_2.setFont(font)
self.textBrowser_2.setObjectName("textBrowser_2")
self.Clear = QtWidgets.QPushButton(self.centralwidget)
self.Clear.setGeometry(QtCore.QRect(140, 330, 101, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.Clear.setFont(font)
self.Clear.setObjectName("Clear")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(270, 330, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 60, 91, 61))
font = QtGui.QFont()
font.setPointSize(12)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(250, 70, 81, 41))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(360, 60, 91, 61))
font = QtGui.QFont()
font.setPointSize(12)
self.comboBox_2.setFont(font)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
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)
This is the reading function:
def Connect(self,MainWindow):
port=self.comboBox.currentText()
baudrate=self.comboBox_2.currentText()
serialPort = serial.Serial(port = port, baudrate=baudrate,
bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
serialString = " "
while(1):
if(serialPort.in_waiting > 0):
serialString = serialPort.readline()
print(serialString.decode('ascii'))
serialPort.write(b"Thank you for sending data \r\n")
So to append any text to the textBrowser simply use this line of code:
self.textBrowser.append(serialString.decode('ascii')) #Append text to the GUI
But you are using while loop inside a GUI. This wil cause the
whole GUI to freeze and your app won't responded to any of your
input.
You have to use threading for you to don't freeze the GUI.
Assert this to your code:
import Threading
...
#This should go instaed of the line 140: self.pushButton.clicked.connect(self.Connect)
self.pushButton.clicked.connect(lambda: Threading.Thread(target = self.Connect).start())
...

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

Categories