I am creating a signup and login GUI in pyqt5. I have used Qt Designer to generate the ui. I am getting user information using QLineEdit in signup form. How can i access the information in login window so that a user can login.
My ui python files are:
signup:
class Ui_SignUp(object):
def setupUi(self, SignUp):
SignUp.setObjectName("SignUp")
SignUp.resize(800, 600)
SignUp.setStyleSheet("background-color: rgb(149, 149, 149);")
self.centralwidget = QtWidgets.QWidget(SignUp)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap("user.png"))
self.label_2.setScaledContents(True)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_3.addItem(spacerItem)
self.label_5 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.verticalLayout_3.addWidget(self.label_5)
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setObjectName("lineEdit_4")
self.verticalLayout_3.addWidget(self.lineEdit_4)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.verticalLayout_3.addWidget(self.label_4)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.verticalLayout.addWidget(self.lineEdit_3)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setStyleSheet("")
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.label_7 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.verticalLayout.addWidget(self.label_7)
self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_6.setObjectName("lineEdit_6")
self.verticalLayout.addWidget(self.lineEdit_6)
self.label_6 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.verticalLayout.addWidget(self.label_6)
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setObjectName("lineEdit_5")
self.verticalLayout.addWidget(self.lineEdit_5)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(255, 255, 255);")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.horizontalLayout_2.addLayout(self.verticalLayout_3)
SignUp.setCentralWidget(self.centralwidget)
self.retranslateUi(SignUp)
QtCore.QMetaObject.connectSlotsByName(SignUp)
def retranslateUi(self, SignUp):
_translate = QtCore.QCoreApplication.translate
SignUp.setWindowTitle(_translate("SignUp", "MainWindow"))
self.label_5.setText(_translate("SignUp", "First Name:"))
self.lineEdit_4.setPlaceholderText(_translate("SignUp", "Please enter your first name"))
self.label_4.setText(_translate("SignUp", "Last Name:"))
self.lineEdit_3.setPlaceholderText(_translate("SignUp", "Please enter your last name"))
self.label.setText(_translate("SignUp", "Username:"))
self.lineEdit.setPlaceholderText(_translate("SignUp", "Please choose your Username"))
self.label_3.setText(_translate("SignUp", "Password (must be between 6-20 characters. must include letters and numbers)"))
self.lineEdit_2.setPlaceholderText(_translate("SignUp", "Please choose your Password"))
self.label_7.setText(_translate("SignUp", "Retype Password"))
self.lineEdit_6.setPlaceholderText(_translate("SignUp", "Please retype your password"))
self.label_6.setText(_translate("SignUp", "Email:"))
self.lineEdit_5.setPlaceholderText(_translate("SignUp", "Please enter your email address"))
self.pushButton_2.setText(_translate("SignUp", "Go to Login"))
self.pushButton.setText(_translate("SignUp", "Sign Up"))
login:
class Ui_Welcome(object):
def setupUi(self, Welcome):
Welcome.setObjectName("Welcome")
Welcome.resize(800, 600)
Welcome.setStyleSheet("background-color: rgb(149, 149, 149);")
self.centralwidget = QtWidgets.QWidget(Welcome)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap("logo.png"))
self.label_2.setScaledContents(True)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setStyleSheet("")
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(255, 255, 255);")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.horizontalLayout_2.addLayout(self.verticalLayout_3)
Welcome.setCentralWidget(self.centralwidget)
self.retranslateUi(Welcome)
QtCore.QMetaObject.connectSlotsByName(Welcome)
def retranslateUi(self, Welcome):
_translate = QtCore.QCoreApplication.translate
Welcome.setWindowTitle(_translate("Welcome", "MainWindow"))
self.label.setText(_translate("Welcome", "Username:"))
self.lineEdit.setPlaceholderText(_translate("Welcome", "Please enter your Username"))
self.label_3.setText(_translate("Welcome", "Password:"))
self.lineEdit_2.setPlaceholderText(_translate("Welcome", "Please enter your Password"))
self.pushButton_2.setText(_translate("Welcome", "Go to Sign Up"))
self.pushButton.setText(_translate("Welcome", "Login"))
currently i am storing the values as csv and later access them. But for this i have to restart my app.
class SignUp(QtWidgets.QMainWindow, Ui_SignUp):
def __init__(self):
super(SignUp,self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.createUser)
def changeWin(self):
self.newW = QtWidgets.QMainWindow()
self.ui = Ui_LogIn()
self.ui.setupUi(self.newW)
self.newW.show()
self.hide()
def createUser(self):
if self.lineEdit_2.text() == self.lineEdit_6.text():
values = [self.lineEdit.text(), self.lineEdit_2.text(),
self.lineEdit_6.text(), self.lineEdit_5.text()]
df = pd.DataFrame()
df["user_info"] = values
df.to_csv("user_info.csv", index = False)
self.changeWin()
else:
msg = QMessageBox.warning(None, "Error", "passwords do not match" )
class LogIn(QtWidgets.QMainWindow, Ui_Welcome):
def __init__(self):
super(LogIn,self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.HandleLogin)
def HandleLogin(self):
UName = self.lineEdit.text()
UPass = self.lineEdit_2.text()
if self.Validate(UName, UPass):
self.changeWin()
else:
QMessageBox.warning(self, 'Error', 'Bad user or password')
def Validate(self, UName, UPass):
RetVal = False
df1 = pd.read_csv("user_info.csv")
UN = df1["user_info"][0]
UP = df1["user_info"][1]
if (UName == UN and UPass == UP):
RetVal = True
return RetVal
def SwitchWindow(w1, w2):
w1.hide()
w2.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
loginW = LogIn()
signupW = SignUp()
loginW.pushButton_2.clicked.connect(lambda: SwitchWindow(loginW, signupW))
signupW.pushButton_2.clicked.connect(lambda: SwitchWindow(signupW, loginW))
loginW.show()
sys.exit(app.exec_())
Is there a way that for the first time i can access the input values from signup to login without restarting my app? Because from second time i can access them from csv.
Yes, there is a way you can access the data obtained in the login form. You can use signals and slots.
QObjects have signals and slots, signals are things that are emitted in response to an event. So when an event happens to an object that particular object emits a signal.
We can connect a signal to a slot, a slot is something that triggers some piece of functionality in a widget.
Here is a small example.
class SignUp(qtw.QWidget):
submitted = qtc.pyqtSignal(str,str)
def __init__(self):
super().__init__()
self.setLayout(qtw.QVBoxLayout())
self.user_name = qtw.QLineEdit()
self.password = qtw.QLineEdit()
self.submit = qtw.QPushButton("sign up")
self.user_name.setText("username")
self.password.setText("password")
self.layout().addWidget(self.user_name)
self.layout().addWidget(self.password)
self.layout().addWidget(self.submit)
self.submit.clicked.connect(self.on_submit)
self.show()
def on_submit(self):
self.submitted.emit(self.user_name.text(),self.password.text())
self.close()
submitted = qtc.pyqtSignal(str,str) is a signal. When the sign up button is clicked, it will emit a signal and connect it to the on_submit(self) method.
class Loginwindow(qtw.QWidget):
def __init__(self):
super().__init__()
self.setLayout(qtw.QVBoxLayout())
self.user_name = qtw.QLineEdit()
self.password = qtw.QLineEdit()
self.login = qtw.QPushButton("Log in")
self.signup = qtw.QPushButton("sign up")
self.layout().addWidget(self.user_name)
self.layout().addWidget(self.password)
self.layout().addWidget(self.login)
self.layout().addWidget(self.signup)
self.signup.clicked.connect(self.signupwindow)
self.show()
#qtc.pyqtSlot(str,str)
def updatemessages(self,username,password):
self.user_name.setText(username)
self.password.setText(password)
def signupwindow(self):
self.SignUp = SignUp()
self.SignUp.submitted.connect(self.updatemessages)
self.SignUp.show()
In signupwindow() method we are opening the SignUp window and we are connecting the submit signal with the updatemessages slots.
For more information, check these videos out.
Moving Data Between
Windows
Introduction to Signal &
Slots
Related
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())
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_())
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_())
I created two .ui files using pyqt designer: one being a Main Window and another being a Widget. Whenever I try to add a widget to the scroll area in my Main Window, it doesn't display on the screen and i can't figure out why.
Sorry if it's a stupid question, i'm still learning pyqt5 and pyqt designer. Any help would be appreciated, Thank you.
CODE:
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.ui=Ui_Reddit()
self.ui.setupUi(self)
self.setStyle(QStyleFactory.create('Cleanlooks'))
#self.setGeometry(300, 300, 600, 600)
self.setWindowTitle("Reddit")
self.show()
self.mainWindow()
def mainWindow(self):
#to handle all the api calls using praw
self.x = RedditApi()
self.printSubmissions()
def printSubmissions(self):
#Gets the list of all submission titles to be displayed
#TO DO: Get and add other things like points and comments
self.submissions = self.x.showSubmissions()
for submission in self.submissions:
card = QWidget()
card_ui = Ui_Card()
card_ui.setupUi(card)
card_ui.title.setText(submission)
self.ui.verticalLayout_2.addWidget(card)
Ui_Reddit:
class Ui_Reddit(object):
def setupUi(self, Reddit):
Reddit.setObjectName("Reddit")
Reddit.resize(800, 600)
Reddit.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(Reddit)
self.centralwidget.setObjectName("centralwidget")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(10, 60, 781, 531))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 779,
529))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayoutWidget_2 =
QtWidgets.QWidget(self.scrollAreaWidgetContents)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(-1, -1, 811,
551))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 =
QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 801, 51))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout =
QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setIndent(5)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.comboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
self.comboBox.setObjectName("comboBox")
self.horizontalLayout.addWidget(self.comboBox)
spacerItem = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.search_bar = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.search_bar.setText("")
self.search_bar.setObjectName("search_bar")
self.horizontalLayout.addWidget(self.search_bar)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem1 = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
Reddit.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(Reddit)
self.statusbar.setObjectName("statusbar")
Reddit.setStatusBar(self.statusbar)
self.retranslateUi(Reddit)
QtCore.QMetaObject.connectSlotsByName(Reddit)
def retranslateUi(self, Reddit):
_translate = QtCore.QCoreApplication.translate
Reddit.setWindowTitle(_translate("Reddit", "MainWindow"))
self.label.setText(_translate("Reddit", "TextLabel"))
self.search_bar.setPlaceholderText(_translate("Reddit", "Search"))
self.pushButton.setText(_translate("Reddit", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Reddit = QtWidgets.QMainWindow()
ui = Ui_Reddit()
ui.setupUi(Reddit)
Reddit.show()
sys.exit(app.exec_())
Ui_Card:
class Ui_Card(object):
def setupUi(self, Card):
Card.setObjectName("Card")
Card.setWindowModality(QtCore.Qt.NonModal)
Card.setEnabled(True)
Card.resize(771, 134)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(Card.sizePolicy().hasHeightForWidth())
Card.setSizePolicy(sizePolicy)
self.title = QtWidgets.QLabel(Card)
self.title.setGeometry(QtCore.QRect(6, 10, 761, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.title.setFont(font)
self.title.setObjectName("title")
self.user = QtWidgets.QLabel(Card)
self.user.setGeometry(QtCore.QRect(20, 40, 47, 13))
font = QtGui.QFont()
font.setPointSize(7)
self.user.setFont(font)
self.user.setObjectName("user")
self.label_3 = QtWidgets.QLabel(Card)
self.label_3.setGeometry(QtCore.QRect(70, 30, 20, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.subreddit = QtWidgets.QLabel(Card)
self.subreddit.setGeometry(QtCore.QRect(90, 40, 47, 13))
font = QtGui.QFont()
font.setPointSize(7)
self.subreddit.setFont(font)
self.subreddit.setObjectName("subreddit")
self.horizontalLayoutWidget = QtWidgets.QWidget(Card)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 60, 771,
31))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout =
QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.comments = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.comments.setObjectName("comments")
self.horizontalLayout.addWidget(self.comments)
self.upvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.upvote.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("upvoteArrow.jpg"), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
self.upvote.setIcon(icon)
self.upvote.setFlat(True)
self.upvote.setObjectName("upvote")
self.horizontalLayout.addWidget(self.upvote)
self.downvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.downvote.setFlat(True)
self.downvote.setObjectName("downvote")
self.horizontalLayout.addWidget(self.downvote)
spacerItem1 = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.line = QtWidgets.QFrame(Card)
self.line.setGeometry(QtCore.QRect(0, 100, 771, 21))
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setLineWidth(3)
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setObjectName("line")
self.retranslateUi(Card)
QtCore.QMetaObject.connectSlotsByName(Card)
def retranslateUi(self, Card):
_translate = QtCore.QCoreApplication.translate
Card.setWindowTitle(_translate("Card", "Form"))
self.title.setText(_translate("Card", "TextLabel"))
self.user.setText(_translate("Card", "TextLabel"))
self.label_3.setText(_translate("Card", "-"))
self.subreddit.setText(_translate("Card", "TextLabel"))
self.comments.setText(_translate("Card", "TextLabel"))
self.downvote.setText(_translate("Card", "downvote"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Card = QtWidgets.QWidget()
ui = Ui_Card()
ui.setupUi(Card)
Card.show()
sys.exit(app.exec_())
I wanted to know how the Matplotlib plotting works on PyQT5 desktop app.
I tried several ways so that the graph would be plotted but no help.
I have used a print function so that i can atleast know that the graph is plotted. It prints as plotted but the graph is not displayed. Need some help here. Below is my Code
'''
Created on Oct 8, 2018
#author: kjohn
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox, QMainWindow, QVBoxLayout
from PyQt5.QtGui import QIcon
import os
import csv
import matplotlib
from matplotlib import figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class MatplotlibFigure(QWidget):
def __init__(self):
super().__init__()
self.figure = matplotlib.figure.Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
def plot(self):
self.figure.clf()
ax = self.figure.add_subplot(111)
x = [i for i in range(100)]
y = [i**0.5 for i in x]
ax.plot(x, y, 'g*-')
self.canvas.draw_idle()
print('PLOTTED')
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1001, 651)
font = QtGui.QFont()
font.setFamily("Segoe UI")
Form.setFont(font)
Form.setMouseTracking(True)
Form.setWindowTitle("Data Analytical Tool Kit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(440, 70, 121, 25))
self.pushButton.clicked.connect(self.inputfileselector)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setMouseTracking(True)
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(670, 70, 241, 30))
font = QtGui.QFont()
font.setFamily("Segoe UI")
self.comboBox.setFont(font)
self.comboBox.setMouseTracking(True)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem('Site Classifier')
self.comboBox.addItem('Account Classifier')
self.comboBox.addItem('Meter Classifier')
self.comboBox.addItem('EMS - Audit Engine')
self.comboBox.addItem('Budget Forecaster')
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 70, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(True)
self.lineEdit.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(21, 51, 91, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(671, 52, 108, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(669, 161, 121, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.plot_data)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(792, 161, 121, 31))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(440, 117, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setMouseTracking(True)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.outputfolderselector)
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 117, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setAutoFillBackground(True)
self.lineEdit_2.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(21, 97, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(20, 210, 411, 421))
self.textEdit.setObjectName("textEdit")
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(670, 116, 275, 31))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(440, 166, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setMouseTracking(True)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.resultfolderselector)
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 166, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(7)
font.setPointSize(10)
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setAutoFillBackground(True)
self.lineEdit_3.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_3.setText("")
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(22, 146, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.figure = matplotlib.figure.Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, Form)
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(440, 210, 541, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout.addWidget(self.canvas)
self.verticalLayout.addWidget(self.toolbar)
self.figure = MatplotlibFigure()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Browse File"))
self.label.setText(_translate("Form", "Input File Upload"))
self.label_2.setText(_translate("Form", "Select Classifier Type"))
self.pushButton_2.setText(_translate("Form", "Submit"))
self.pushButton_3.setText(_translate("Form", "Cancel"))
self.pushButton_4.setText(_translate("Form", "Select Folder"))
self.label_3.setText(_translate("Form", "Output Folder for Logs"))
self.pushButton_5.setText(_translate("Form", "Select Folder"))
self.label_4.setText(_translate("Form", "Result Folder"))
Form.setWindowIcon(QtGui.QIcon('icon.png'))
Form.setWindowTitle('Data Analysis Tool Kit')
Form.setGeometry(180,50,1001,651)
Form.setFixedSize(1001,651)
Form.setWindowFlags( QtCore.Qt.CustomizeWindowHint | QtCore.Qt.MSWindowsFixedSizeDialogHint |QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint )
def inputfileselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filename = QtWidgets.QFileDialog.getOpenFileName(None, 'Select the Input File', path,'All Files(*.csv*)')
if filename is filename:
self.lineEdit.setText(str(filename))
else:
self.lineEdit.setText("No file was selected.")
QtWidgets.QMessageBox.critical(Form, "Alert", "No file was selected.", QtWidgets.QMessageBox.Close)
def outputfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_2.setText(directory)
else:
self.lineEdit_2.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.", QtWidgets.QMessageBox.Close)
def resultfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_3.setText(directory)
else:
self.lineEdit_3.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.", QtWidgets.QMessageBox.Close)
def plot_data(self):
self.figure.plot()
self.canvas.setParent(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
First you have created a class MatplotlibFigure for matlabplot, at the same time you define matlab function in class Ui_Form, this is an error. to solve your problem,
We need to define two functions inside class MatplotlibFigure, the first func initializewidget() which defines canvas widget and figure frame for matlabplot with QGridLayout places and arranges them. Then the second func plot() draws the input data.
def initializewidget(self):
gridlayout = QGridLayout()
self.setLayout(gridlayout)
self.figure = plt.figure(figsize=(5,5))
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas,self)
gridlayout.addWidget(self.canvas,1,0,1,2)
gridlayout.addWidget(self.toolbar,0,0,1,2)
And finally Matlabplot widget should be added to main widget layout.
self.matplot = MatplotlibFigure()
self.verticalLayout.addWidget(self.matplot)
Matplotlib.pyplot is used for getting better graphs and even plots could customize with different styles.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
The working current screen
The working code
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit,
QFileDialog, QMessageBox, QMainWindow, QVBoxLayout, QGridLayout
from PyQt5.QtGui import QIcon
import os
import csv
import matplotlib
from matplotlib import figure
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class MatplotlibFigure(QWidget):
def __init__(self):
super().__init__()
self.initializewidget()
self.plot()
def initializewidget(self):
gridlayout = QGridLayout()
self.setLayout(gridlayout)
self.figure = plt.figure(figsize=(5,5))
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas,self)
gridlayout.addWidget(self.canvas,1,0,1,2)
gridlayout.addWidget(self.toolbar,0,0,1,2)
def plot(self):
ax = self.figure.add_subplot(111)
x = [i for i in range(100)]
y = [1*i for i in x]
ax.plot(x, y, 'g*-')
self.canvas.draw()
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1001, 651)
font = QtGui.QFont()
font.setFamily("Segoe UI")
Form.setFont(font)
Form.setMouseTracking(True)
Form.setWindowTitle("Data Analytical Tool Kit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(440, 70, 121, 25))
self.pushButton.clicked.connect(self.inputfileselector)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setMouseTracking(True)
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(670, 70, 241, 30))
font = QtGui.QFont()
font.setFamily("Segoe UI")
self.comboBox.setFont(font)
self.comboBox.setMouseTracking(True)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem('Site Classifier')
self.comboBox.addItem('Account Classifier')
self.comboBox.addItem('Meter Classifier')
self.comboBox.addItem('EMS - Audit Engine')
self.comboBox.addItem('Budget Forecaster')
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 70, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(True)
self.lineEdit.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(21, 51, 91, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(671, 52, 108, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(669, 161, 121, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.plot_data)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(792, 161, 121, 31))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(440, 117, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setMouseTracking(True)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.outputfolderselector)
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 117, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setAutoFillBackground(True)
self.lineEdit_2.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(21, 97, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(20, 210, 411, 421))
self.textEdit.setObjectName("textEdit")
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(670, 116, 275, 31))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(440, 166, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setMouseTracking(True)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.resultfolderselector)
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 166, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(7)
font.setPointSize(10)
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setAutoFillBackground(True)
self.lineEdit_3.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_3.setText("")
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(22, 146, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.matplot = MatplotlibFigure()
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(440, 210, 541, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
# self.verticalLayout.addWidget(self.matplot) # comment this line
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Browse File"))
self.label.setText(_translate("Form", "Input File Upload"))
self.label_2.setText(_translate("Form", "Select Classifier Type"))
self.pushButton_2.setText(_translate("Form", "Submit"))
self.pushButton_3.setText(_translate("Form", "Cancel"))
self.pushButton_4.setText(_translate("Form", "Select Folder"))
self.label_3.setText(_translate("Form", "Output Folder for Logs"))
self.pushButton_5.setText(_translate("Form", "Select Folder"))
self.label_4.setText(_translate("Form", "Result Folder"))
Form.setWindowIcon(QtGui.QIcon('icon.png'))
Form.setWindowTitle('Data Analysis Tool Kit')
Form.setGeometry(180,50,1001,651)
Form.setFixedSize(1001,651)
Form.setWindowFlags( QtCore.Qt.CustomizeWindowHint |
QtCore.Qt.MSWindowsFixedSizeDialogHint |QtCore.Qt.WindowCloseButtonHint |
QtCore.Qt.WindowMinimizeButtonHint )
def inputfileselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filename = QtWidgets.QFileDialog.getOpenFileName(None, 'Select the Input
File', path,'All Files(*.csv*)')
if filename is filename:
self.lineEdit.setText(str(filename))
else:
self.lineEdit.setText("No file was selected.")
QtWidgets.QMessageBox.critical(Form, "Alert", "No file was selected.",
QtWidgets.QMessageBox.Close)
def outputfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working
directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_2.setText(directory)
else:
self.lineEdit_2.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.",
QtWidgets.QMessageBox.Close)
def resultfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working
directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_3.setText(directory)
else:
self.lineEdit_3.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.",
QtWidgets.QMessageBox.Close)
def plot_data(self):
# self.figure.plot() # comment this line
# self.canvas.setParent(self) # comment this line
self.verticalLayout.addWidget(self.matplot) # <--- Add this line
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())