I am writing GUI app with the help of Qt Designer. The app consists of multiple windows, so when the user clicks the push button on one window, the next window should open and the current window should hide. I achieved this feature for going from window 1 to window 2. But I am not able to do this to move from window 2 to window 3. I think this might be because the window 2 gets user arguments.
Is there any way to hide the window which gets user arguments?
The code is as follows:
Class for main window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import user_data
class MainWindow(object):
def user_data(self):
self.user_data_window = QtWidgets.QMainWindow()
self.next_window = user_data.Ui_UserData()
self.next_window.setupUi(self.user_data_window)
main_window.hide()
self.user_data_window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 100, 361, 81))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.user_data)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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", "Main Window"))
self.label.setText(_translate("MainWindow", " Manager"))
self.pushButton.setText(_translate("MainWindow", "Lets Start!"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(main_window)
main_window.show()
sys.exit(app.exec_())
user_data.py ---> window 2
from PyQt5 import QtCore, QtGui, QtWidgets
import start_recording
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox_speaker_data = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_speaker_data.setGeometry(QtCore.QRect(80, 30, 481, 381))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(84)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.groupBox_speaker_data.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.groupBox_speaker_data.setFont(font)
self.groupBox_speaker_data.setObjectName("groupBox_speaker_data")
self.label_name = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_name.setGeometry(QtCore.QRect(30, 60, 47, 13))
self.label_name.setObjectName("label_name")
self.label_age = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_age.setGeometry(QtCore.QRect(30, 120, 47, 13))
self.label_age.setObjectName("label_age")
self.label_id = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_id.setGeometry(QtCore.QRect(30, 90, 47, 13))
self.label_id.setObjectName("label_id")
self.label_gender = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_gender.setGeometry(QtCore.QRect(30, 150, 47, 13))
self.label_gender.setObjectName("label_gender")
self.lineEdit_name = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_name.setGeometry(QtCore.QRect(110, 60, 141, 20))
self.lineEdit_name.setObjectName("lineEdit_name")
self.lineEdit_id = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_id.setGeometry(QtCore.QRect(110, 90, 141, 20))
self.lineEdit_id.setObjectName("lineEdit_id")
self.spinBox_age = QtWidgets.QSpinBox(self.groupBox_speaker_data)
self.spinBox_age.setGeometry(QtCore.QRect(110, 120, 42, 22))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(18)
sizePolicy.setVerticalStretch(78)
self.spinBox_age.setSizePolicy(sizePolicy)
self.spinBox_age.setMinimum(18)
self.spinBox_age.setObjectName("spinBox_age")
self.comboBox_gender = QtWidgets.QComboBox(self.groupBox_speaker_data)
self.comboBox_gender.setGeometry(QtCore.QRect(110, 150, 69, 22))
self.comboBox_gender.setObjectName("comboBox_gender")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.pushButton = QtWidgets.QPushButton(self.groupBox_speaker_data)
self.pushButton.setGeometry(QtCore.QRect(370, 330, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_record)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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", "User Data"))
self.groupBox_speaker_data.setTitle(_translate("MainWindow", "Speaker Data"))
self.label_name.setText(_translate("MainWindow", "Name:"))
self.label_age.setText(_translate("MainWindow", "Age:"))
self.label_id.setText(_translate("MainWindow", "ID:"))
self.label_gender.setText(_translate("MainWindow", "Gender:"))
self.comboBox_gender.setItemText(0, _translate("MainWindow", "Male"))
self.comboBox_gender.setItemText(1, _translate("MainWindow", "Female"))
self.comboBox_gender.setItemText(2, _translate("MainWindow", "Unknown"))
self.pushButton.setText(_translate("MainWindow", "NEXT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_UserData()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
start_recording.py ----> window 3
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StartRecording(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 120, 331, 91))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(18)
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(260, 200, 101, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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.label.setText(_translate("MainWindow", "Lets begin"))
self.label_2.setText(_translate("MainWindow", "xxx!"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_StartRecording()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
^^^^^^^^ ---- there is no 'MainWindow' variable available to class, hence it doesn't know what to hide here.
Solution is:
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This part is now working!
"""
self.mw.hide()
self.window.show()
def setupUi(self, MainWindow):
self.mw = MainWindow
# ^^^^^^^^ - You create class variable for mainwindow object
Related
The function genPassword is going to generate a random password, all I want is getting the value returned from the function and putting it into the screen.
This is the full code.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(553, 600)
MainWindow.setMouseTracking(False)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 60, 371, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.generate = QtWidgets.QPushButton(self.centralwidget)
self.generate.setEnabled(True)
self.generate.setGeometry(QtCore.QRect(190, 220, 161, 41))
font = QtGui.QFont()
font.setPointSize(20)
self.generate.setFont(font)
self.generate.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.generate.setObjectName("generate")
Here, I wish to put my password,
Lambda didn't work
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(210, 140, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(160, 320, 231, 80))
self.widget.setObjectName("widget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 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", "Generator"))
self.label.setText(_translate("MainWindow", "Enter the password length"))
self.generate.setText(_translate("MainWindow", "Generate"))
Here is the function,
"I want to take the length argument from the LineEdit widget"
def genPassword(self, length):
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
resources = list(ascii_lowercase) + list(ascii_uppercase) + nums
result = ""
for i in range(length):
result += str(choice(resources))
return result
if __name__ == "__main__":
import sys
from string import ascii_lowercase, ascii_uppercase
from random import choice
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
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)
How to reposition the widgets when window maximized?
from PyQt5 import QtCore, QtGui, QtWidgets
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.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(350, 100, 81, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(270, 130, 81, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 160, 81, 21))
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(370, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(370, 160, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_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.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
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_())
You can read about layout management in PyQt5 here: http://zetcode.com/gui/pyqt5/layout/
Try as an option:
from PyQt5 import QtCore, QtGui, QtWidgets
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.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(350, 100, 81, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(270, 130, 81, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 160, 81, 21))
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(370, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(370, 160, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_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.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
formLayout = QtWidgets.QFormLayout()
formLayout.addRow(self.label_2, self.lineEdit)
formLayout.addRow(self.label_3, self.lineEdit_2)
layoutV = QtWidgets.QVBoxLayout(self.centralwidget)
layoutV.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
layoutV.addLayout(formLayout)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have the parallel thread that read 10 uart and prints the data in pyqt5 gui. But the issue I am facing is those groupbox get updated when all 10 jobs completed but not instantly.
EDIT: To achieve parallelism I am using joblib not pyqt thread.
What I want to achieve is change the state of groubox attributes such as a label, pushbutton text, and colour but I don't find a good example how to trigger a signal emit for the groupbox.
Do I need to emit the individual label, buttons in the groubox or I can emit as one groupbox object?
This is a minimalistic code, where it has just two groupbx. Can someone help how to emit the signal from groubox and catch that change, i.e the state of label, button text, and colour in the below code?
Below example update the groubox without any issue but in real each g1 method is uart and it's blocking call. I wait till all jobs to complete to see the changes on my GUI.
Code
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, pyqtSignal
class Ui_MainWindow(object):
changedValue = pyqtSignal(int)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
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.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
def g1(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText(_translate("MainWindow", "Pass"))
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
def g2(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
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 I recommend you read Using the Generated Code from the docs. PyQt in this post recommends not modifying the code generated by Qt Designer since it is not a widget it is only a class that is used to fill a widget, instead it is better to create another class based on the chosen form and implement the logic there, since for example your code has a serious error: the signals can only be created in a QObject like the widgets but Ui_MainWindow is not.
Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
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.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
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.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
changedValue = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
#QtCore.pyqtSlot()
def g1(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText("Pass")
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
#QtCore.pyqtSlot()
def g2(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Im pretty new to qt designer using python. Im having a hard time inserting data into mysql database from radio button and combo box wigdets. Can you give me an example how to insert data from this wigdets and putting it into variable in order to use into query? Thanks
from PyQt5 import QtCore, QtGui, QtWidgets
import pymysql
class Ui_MainWindow(object):
def insertData(self):
dropDown = self.comboBox.addItems(self.comboBox)
conn = pymysql.connect("localhost","root","warren110598","dss")
with conn:
cur = conn.cursor()
cur.execute("INSERT INTO ml (rank) values ('%s')" %(''.join(dropDown)))
print("euuu")
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(426, 302)
MainWindow.setStyleSheet("background-color: rgb(242, 255, 225);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(230, 120, 69, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 120, 111, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 170, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.insertData)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 426, 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.comboBox.setItemText(0, _translate("MainWindow", "dae"))
self.comboBox.setItemText(1, _translate("MainWindow", "eu"))
self.comboBox.setItemText(2, _translate("MainWindow", "mapondo na"))
self.label.setText(_translate("MainWindow", "Ma mobile legends?"))
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_())