This question already has answers here:
QtDesigner changes will be lost after redesign User Interface
(2 answers)
Closed 11 months ago.
i was wondering if it possible using QMessagBox in Qmainwindow.
i am getting this error:
Traceback (most recent call last): File
"C:\Users\royqb\desktop\python_learning\softwaremgmt\table.py", line
229, in refresh_warning
msg.question(self,'Warning', 'Are you sure to continue?',msg.Yes | msg.No) TypeError: question(QWidget, str, str, buttons:
Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] =
QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No),
defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton):
argument 1 has unexpected type 'Ui_MainWindow'
in other window which is Qdialog it works just fine.
please help :)
import sqlite3
import smtplib
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
from PyQt5.QtGui import *
from newlogin import *
class Ui_MainWindow(object):
# def __init__(self):
# self.connect = sqlite3.connect('lics.db')
# self.cur = self.connect.cursor()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
# MainWindow.resize(1250, 800)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# self.setWindowTitle('Licenses')
self.new_lic_button = QtWidgets.QPushButton(self.centralwidget)
self.new_lic_button.setGeometry(QtCore.QRect(10, 80, 150, 40))
self.new_lic_button.setStyleSheet("QPushButton#new_lic_button{\n"
"background-color:lightgrey;\n"
"border-radius:5px;\n"
"color:grey;\n"
"border:1px solid darkgrey;\n"
"}\n"
"QPushButton#new_lic_button:pressed{\n"
"background-color:rgba(148, 151, 150, 0.8);\n"
"}\n"
"QPushButton#new_lic_button:hover{\n"
"background-color:rgba(223, 223, 223, 0.8);\n"
"color:rgba(89, 93, 92, 0.8);\n"
"}\n"
"")
self.new_lic_button.setObjectName("ref_data")
self.send_mail_button = QtWidgets.QPushButton(self.centralwidget)
self.send_mail_button.setGeometry(QtCore.QRect(170, 80, 150, 40))
self.send_mail_button.setStyleSheet("QPushButton#send_mail_button{\n"
"background-color:lightgrey;\n"
"border-radius:5px;\n"
"color:grey;\n"
"border:1px solid darkgrey;\n"
"}\n"
"QPushButton#send_mail_button:pressed{\n"
"background-color:rgba(148, 151, 150, 0.8);\n"
"}\n"
"QPushButton#send_mail_button:hover{\n"
"background-color:rgba(223, 223, 223, 0.8);\n"
"color:rgba(89, 93, 92, 0.8);\n"
"}\n"
"")
self.send_mail_button.setObjectName("sub_change")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 150, 1700, 900))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setStyleSheet("background-color:rgba(246, 246, 246, 0.8)")
self.tableWidget.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.tableWidget.setFrameShadow(QtWidgets.QFrame.Sunken)
self.tableWidget.setLineWidth(1)
self.tableWidget.setWordWrap(True)
self.tableWidget.setRowCount(0)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(8)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
item.setBackground(QtGui.QColor(230, 230, 230))
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
self.tableWidget.setHorizontalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
item.setFont(font)
self.tableWidget.setHorizontalHeaderItem(7, item)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(750, 10, 280, 51))
font = QtGui.QFont()
font.setFamily("Segoe script")
font.setPointSize(10)
self.label.setFont(font)
self.label.setStyleSheet("text-size:20px;")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(0, 0, 1920, 1000))
self.widget.setStyleSheet("border-radius:20px;\n"
"background-color:rgb(187, 247, 228,0.8);")
self.widget.setObjectName("widget")
self.widget.raise_()
self.new_lic_button.raise_()
self.send_mail_button.raise_()
self.tableWidget.raise_()
self.label.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1016, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEfit = QtWidgets.QMenu(self.menubar)
self.menuEfit.setObjectName("menuEfit")
MainWindow.setMenuBar(self.menubar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.LeftToolBarArea, self.toolBar)
self.actionShow_All = QtWidgets.QAction(MainWindow)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("pic/icons/show.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionShow_All.setIcon(icon3)
self.actionShow_All.setObjectName("actionShow_All")
self.actionExport_xlsx = QtWidgets.QAction(MainWindow)
self.actionExport_xlsx.setObjectName("actionExport_xlsx")
self.actionExport_Xlsx = QtWidgets.QAction(MainWindow)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("pic/icons/xlsx.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionExport_Xlsx.setIcon(icon)
self.actionExport_Xlsx.setObjectName("actionExport_Xlsx")
self.actionEmail = QtWidgets.QAction(MainWindow)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("pic/icons/mail.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionEmail.setIcon(icon1)
self.actionEmail.setObjectName("actionEmail")
self.actionNew_License = QtWidgets.QAction(MainWindow)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("pic/icons/plus.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionNew_License.setIcon(icon2)
self.actionNew_License.setObjectName("actionNew_License")
self.menuFile.addAction(self.actionExport_xlsx)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEfit.menuAction())
self.toolBar.addAction(self.actionNew_License)
self.toolBar.addAction(self.actionExport_Xlsx)
self.toolBar.addAction(self.actionEmail)
self.toolBar.addAction(self.actionShow_All)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#send mail from tool bar
self.actionEmail.triggered.connect(self.send_mail)
#table width
MainWindow.setGeometry(QtCore.QRect(10, 100, 1920, 900))
self.tableWidget.setColumnWidth(0, 180)
self.tableWidget.setColumnWidth(1, 250)
self.tableWidget.setColumnWidth(2, 250)
self.tableWidget.setColumnWidth(3, 100)
self.tableWidget.setColumnWidth(4, 150)
self.tableWidget.setColumnWidth(5, 150)
self.tableWidget.setColumnWidth(6, 100)
self.tableWidget.setColumnWidth(7, 500)
self.tableWidget.setHorizontalHeaderLabels(
['Company', 'License type', 'Supplier', 'Interval', 'Last update', 'Next update', 'Time left'])
self.loaddata()
self.new_lic_button.clicked.connect(self.refresh_warning)
# self.send_mail_button.clicked.connect(self.submit_changes)
def refresh_warning(self):
msg = QMessageBox()
# msg.setWindowTitle(self,"Warning")
# msg.setText(self, 'Any changes you made will be lost!')
msg.question(self,'Warning', 'Are you sure to continue?',msg.Yes | msg.No)
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg.exec_()
if self.new_lic_button.clicked:
ask = MainWindow.QMessageBox.question(MainWindow,"If you made changes they will be lost! \nUse 'Submit Changes' button to save your changes, continue?",QMessageBox.Yes|QMessageBox.No)
if ask == QMessageBox.Yes:
self.loaddata()
def loaddata(self):
connect = sqlite3.connect('lics.db')
cur = connect.cursor()
sqlquery = ('select company,lic_name,supplier, frequency,last_update, next_update, time_left, lic_path from licenses WHERE time_left_int < 30 order by time_left_int asc ')
cur.execute(sqlquery)
rows = cur.fetchall()
self.tableWidget.setRowCount(len(rows))
tablerow = 0
for row in cur.execute(sqlquery):
self.tableWidget.setItem(tablerow, 0, QtWidgets.QTableWidgetItem(row[0]))
self.tableWidget.setItem(tablerow, 1, QtWidgets.QTableWidgetItem(row[1]))
self.tableWidget.setItem(tablerow, 2, QtWidgets.QTableWidgetItem(row[2]))
self.tableWidget.setItem(tablerow, 3, QtWidgets.QTableWidgetItem(row[3]))
self.tableWidget.setItem(tablerow, 4, QtWidgets.QTableWidgetItem(row[4]))
self.tableWidget.setItem(tablerow, 5, QtWidgets.QTableWidgetItem(row[5]))
self.tableWidget.setItem(tablerow, 6, QtWidgets.QTableWidgetItem(row[6]))
self.tableWidget.setItem(tablerow, 7, QtWidgets.QTableWidgetItem(row[7]))
tablerow +=1
def send_mail(self):
self.ui2 = Ui_login_page()
user = self.ui2.hashed_user
# print(user)
connect = sqlite3.connect('lics.db')
cur = connect.cursor()
# emailsql = ("select email from users where user = '{}' ".format(user))
# print(emailsql)
smtp_sslobj = smtplib.SMTP_SSL('smtp.gmail.com',465)
smtp_sslobj.ehlo()
# smtp_sslobj.starttls()
sent_from = "pythroy#gmail.com"
# self.to_mail = ""
gmail_user = sent_from
sqlq= ('select email_pass from users where email = "{}" '.format(self.to_mail))
gmail_pass = '1qa1qa1qa'
# cur.execute(sqlq)
# print(gmail_pass)
smtp_sslobj.login(sent_from,gmail_pass)
to = self.to_mail
subject ="Expiring licenses"
body = "hello"
smtp_sslobj.sendmail(sent_from, self.to_mail,"hello" )
smtp_sslobj.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.new_lic_button.setText(_translate("MainWindow", "Refresh Data"))
self.send_mail_button.setText(_translate("MainWindow", "Submit Changes"))
self.tableWidget.setSortingEnabled(True)
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Company"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Lic name"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Supplier"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Interval"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Last update"))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "Next update"))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "Time left"))
item = self.tableWidget.horizontalHeaderItem(7)
item.setText(_translate("MainWindow", "Path"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:14pt; font-weight:600; text-decoration: underline;\">Expiring Licenses</span></p></body></html>"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuEfit.setTitle(_translate("MainWindow", "Edit"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actionExport_xlsx.setText(_translate("MainWindow", "Export xlsx"))
self.actionExport_Xlsx.setText(_translate("MainWindow", "Export Xlsx"))
self.actionExport_Xlsx.setToolTip(_translate("MainWindow", "Export to xlsx"))
self.actionExport_Xlsx.setStatusTip(_translate("MainWindow", "Export to xlsx"))
self.actionEmail.setText(_translate("MainWindow", "Email"))
self.actionEmail.setToolTip(_translate("MainWindow", "Send mail"))
self.actionEmail.setStatusTip(_translate("MainWindow", "Send mail"))
self.actionNew_License.setText(_translate("MainWindow", "New License"))
self.actionNew_License.setStatusTip(_translate("MainWindow", "Insert new license"))
self.actionNew_License.setWhatsThis(_translate("MainWindow", "New license"))
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_())
QMessageBox.questions first argument must be QWidget or None, Ui_MainWindow (self) is not a widget, it's a helper class not inherited from QWidget, use self.widget instead.
Related
Complete Code
I am expecting a scroll bar should get auto populate so that user can see the rest of the item.
in that highlighted area I tried
created one widget
added one Scroll Area under that widget
add a from lay out under Scroll Area
adding items to that from layout.
but the scroll area is not working.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName('MainWindow')
MainWindow.resize(817, 588)
sizePolicy = \
QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName('centralwidget')
self.verticalLayoutWidget = \
QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 40, 761,
60))
self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')
self.verticalLayout = \
QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName('verticalLayout')
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout.setObjectName('gridLayout')
self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label.setObjectName('label')
self.gridLayout.addWidget(
self.label,
0,
0,
1,
1,
QtCore.Qt.AlignLeft,
)
self.searchString = \
QtWidgets.QLineEdit(self.verticalLayoutWidget)
self.searchString.setMaximumSize(QtCore.QSize(673, 16777215))
self.searchString.setObjectName('searchString')
self.gridLayout.addWidget(self.searchString, 0, 1, 1, 1)
self.submit = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.submit.setObjectName('submit')
self.gridLayout.addWidget(self.submit, 0, 2, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
self.errorLabel = QtWidgets.QLabel(self.centralwidget)
self.errorLabel.setGeometry(QtCore.QRect(30, 10, 751, 16))
self.errorLabel.setText('')
self.errorLabel.setObjectName('errorLabel')
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(40, 140, 361, 151))
self.groupBox.setObjectName('groupBox')
self.serverList = QtWidgets.QListWidget(self.groupBox)
self.serverList.setEnabled(True)
self.serverList.setGeometry(QtCore.QRect(10, 20, 331, 121))
self.serverList.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.serverList.setResizeMode(QtWidgets.QListView.Fixed)
self.serverList.setObjectName('serverList')
item = QtWidgets.QListWidgetItem()
self.serverList.addItem(item)
item = QtWidgets.QListWidgetItem()
self.serverList.addItem(item)
item = QtWidgets.QListWidgetItem()
self.serverList.addItem(item)
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(410, 140, 361, 151))
self.groupBox_2.setObjectName('groupBox_2')
self.autoDownload = QtWidgets.QCheckBox(self.groupBox_2)
self.autoDownload.setGeometry(QtCore.QRect(10, 30, 121, 20))
self.autoDownload.setObjectName('autoDownload')
self.specificFileFlag = QtWidgets.QCheckBox(self.groupBox_2)
self.specificFileFlag.setGeometry(QtCore.QRect(10, 60, 171, 20))
self.specificFileFlag.setObjectName('specificFileFlag')
self.downloadSelected = QtWidgets.QPushButton(self.groupBox_2)
self.downloadSelected.setGeometry(QtCore.QRect(10, 97, 141, 31))
self.downloadSelected.setObjectName('downloadSelected')
self.searchArchive = QtWidgets.QCheckBox(self.groupBox_2)
self.searchArchive.setGeometry(QtCore.QRect(170, 30, 141, 20))
self.searchArchive.setObjectName('searchArchive')
self.checkBox = QtWidgets.QCheckBox(self.groupBox_2)
self.checkBox.setGeometry(QtCore.QRect(170, 60, 191, 21))
self.checkBox.setObjectName('checkBox')
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(640, 540, 161, 20))
self.label_2.setObjectName('label_2')
self.selectedServer = QtWidgets.QLabel(self.centralwidget)
self.selectedServer.setGeometry(QtCore.QRect(30, 120, 751, 21))
self.selectedServer.setText('')
self.selectedServer.setObjectName('selectedServer')
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(30, 300, 761, 241))
self.widget.setObjectName('widget')
self.scrollArea = QtWidgets.QScrollArea(self.widget)
self.scrollArea.setGeometry(QtCore.QRect(9, 6, 731, 221))
self.scrollArea.setObjectName('scrollArea')
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0,
729, 219))
self.scrollAreaWidgetContents.setObjectName('scrollAreaWidgetContents'
)
self.formLayoutWidget = \
QtWidgets.QWidget(self.scrollAreaWidgetContents)
self.formLayoutWidget.setGeometry(QtCore.QRect(0, 1, 731, 221))
self.formLayoutWidget.setObjectName('formLayoutWidget')
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setContentsMargins(9, 9, 9, 9)
self.formLayout.setObjectName('formLayout')
self.checkBox_2 = QtWidgets.QCheckBox(self.formLayoutWidget)
self.checkBox_2.setObjectName('checkBox_2')
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole,
self.checkBox_2)
self.progressBar = QtWidgets.QProgressBar(self.formLayoutWidget)
self.progressBar.setProperty('value', 24)
self.progressBar.setObjectName('progressBar')
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole,
self.progressBar)
self.checkBox_3 = QtWidgets.QCheckBox(self.formLayoutWidget)
self.checkBox_3.setObjectName('checkBox_3')
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole,
self.checkBox_3)
self.progressBar_2 = \
QtWidgets.QProgressBar(self.formLayoutWidget)
self.progressBar_2.setProperty('value', 24)
self.progressBar_2.setObjectName('progressBar_2')
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole,
self.progressBar_2)
self.checkBox_4 = QtWidgets.QCheckBox(self.formLayoutWidget)
self.checkBox_4.setObjectName('checkBox_4')
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole,
self.checkBox_4)
self.progressBar_3 = \
QtWidgets.QProgressBar(self.formLayoutWidget)
self.progressBar_3.setProperty('value', 24)
self.progressBar_3.setObjectName('progressBar_3')
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole,
self.progressBar_3)
self.checkBox_5 = QtWidgets.QCheckBox(self.formLayoutWidget)
self.checkBox_5.setObjectName('checkBox_5')
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole,
self.checkBox_5)
self.progressBar_4 = \
QtWidgets.QProgressBar(self.formLayoutWidget)
self.progressBar_4.setProperty('value', 24)
self.progressBar_4.setObjectName('progressBar_4')
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole,
self.progressBar_4)
count = 6
c = 0
checkBox = []
while count < 10:
checkBox.append(QtWidgets.QCheckBox(self.formLayoutWidget))
checkBox[c].setObjectName('checkBox_' + str(c))
checkBox[c].setText('test_' + str(c))
self.formLayout.setWidget(count,
QtWidgets.QFormLayout.LabelRole, checkBox[c])
self.formLayout.setWidget(count,
QtWidgets.QFormLayout.FieldRole,
QtWidgets.QProgressBar(self.formLayoutWidget))
count = count + 1
c = c + 1
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
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', 'MainWindow'
))
self.label.setText(_translate('MainWindow',
'<html><head/><body><p><span style=" font-weight:600; color:#000000;">Search String</span><span style=" font-weight:600; color:#ff0000;">*</span></p></body></html>'
))
self.submit.setText(_translate('MainWindow', 'Search'))
self.groupBox.setTitle(_translate('MainWindow', 'Server List'))
__sortingEnabled = self.serverList.isSortingEnabled()
self.serverList.setSortingEnabled(False)
item = self.serverList.item(0)
item.setText(_translate('MainWindow', 'TVOL'))
item = self.serverList.item(1)
item.setText(_translate('MainWindow', 'QA2'))
item = self.serverList.item(2)
item.setText(_translate('MainWindow', 'CVOL'))
self.serverList.setSortingEnabled(__sortingEnabled)
self.groupBox_2.setTitle(_translate('MainWindow',
'Other Configuration'))
self.autoDownload.setToolTip(_translate('MainWindow',
'<html><head/><body><p><br/></p></body></html>'))
self.autoDownload.setText(_translate('MainWindow',
'Auto Download'))
self.specificFileFlag.setToolTip(_translate('MainWindow',
'<html><head/><body><p>write the file name in "Search String" and Click on Search Button</p></body></html>'
))
self.specificFileFlag.setText(_translate('MainWindow',
'Download Specific file'))
self.downloadSelected.setText(_translate('MainWindow',
'Download Selected'))
self.searchArchive.setText(_translate('MainWindow',
'Search in Archive'))
self.checkBox.setToolTip(_translate('MainWindow',
'<html><head/><body><p>This option will try to compress the file in server before downloading</p></body></html>'
))
self.checkBox.setText(_translate('MainWindow',
'Compress Before Download'))
self.label_2.setText(_translate('MainWindow',
'<html><head/><body><p><span style=" color:#969696;">Created by - Sudipto Khan</span></p></body></html>'
))
self.checkBox_2.setText(_translate('MainWindow', 'CheckBox'))
self.checkBox_3.setText(_translate('MainWindow', 'CheckBox'))
self.checkBox_4.setText(_translate('MainWindow', 'CheckBox'))
self.checkBox_5.setText(_translate('MainWindow', 'CheckBox'))
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_())
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
I am trying to update my QLCDNumber, but I failed several times. In my code, you can see that I want to take the input numbers from the double spin boxes and show them as a total sum of the previous land use values in my LCD widget. I have created a function with .value() but it doesn't work properly. The code is bit large, but I highlighted the QLCDNumber part.
Screenshot here:
values to take as inputs
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1039, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 0, 1001, 621))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.groupBox = QtWidgets.QGroupBox(self.tab)
self.groupBox.setGeometry(QtCore.QRect(10, 40, 241, 521))
font = QtGui.QFont()
font.setPointSize(11)
self.groupBox.setFont(font)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.layoutWidget_5 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_5.setGeometry(QtCore.QRect(0, 50, 234, 26))
self.layoutWidget_5.setObjectName("layoutWidget_5")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget_5)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.grass_label = QtWidgets.QLabel(self.layoutWidget_5)
font = QtGui.QFont()
font.setPointSize(12)
self.grass_label.setFont(font)
self.grass_label.setObjectName("grass_label")
self.horizontalLayout_2.addWidget(self.grass_label)
self.grass_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_5)
self.grass_value.setSingleStep(0.05)
self.grass_value.setObjectName("grass_value")
self.horizontalLayout_2.addWidget(self.grass_value)
self.layoutWidget_6 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_6.setGeometry(QtCore.QRect(0, 80, 234, 26))
self.layoutWidget_6.setObjectName("layoutWidget_6")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget_6)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.arable_label = QtWidgets.QLabel(self.layoutWidget_6)
font = QtGui.QFont()
font.setPointSize(12)
self.arable_label.setFont(font)
self.arable_label.setObjectName("arable_label")
self.horizontalLayout_3.addWidget(self.arable_label)
self.arab_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_6)
self.arab_value.setSingleStep(0.05)
self.arab_value.setObjectName("arab_value")
self.horizontalLayout_3.addWidget(self.arab_value)
self.layoutWidget_7 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_7.setGeometry(QtCore.QRect(0, 110, 234, 26))
self.layoutWidget_7.setObjectName("layoutWidget_7")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget_7)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.woodl_label = QtWidgets.QLabel(self.layoutWidget_7)
font = QtGui.QFont()
font.setPointSize(12)
self.woodl_label.setFont(font)
self.woodl_label.setObjectName("woodl_label")
self.horizontalLayout_4.addWidget(self.woodl_label)
self.wood_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_7)
self.wood_value.setSingleStep(0.05)
self.wood_value.setObjectName("wood_value")
self.horizontalLayout_4.addWidget(self.wood_value)
self.layoutWidget_8 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_8.setGeometry(QtCore.QRect(0, 140, 234, 26))
self.layoutWidget_8.setObjectName("layoutWidget_8")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.layoutWidget_8)
self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.mount_label = QtWidgets.QLabel(self.layoutWidget_8)
font = QtGui.QFont()
font.setPointSize(12)
self.mount_label.setFont(font)
self.mount_label.setObjectName("mount_label")
self.horizontalLayout_5.addWidget(self.mount_label)
self.mount_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_8)
self.mount_value.setSingleStep(0.05)
self.mount_value.setObjectName("mount_value")
self.horizontalLayout_5.addWidget(self.mount_value)
self.start_butt = QtWidgets.QPushButton(self.groupBox)
self.start_butt.setGeometry(QtCore.QRect(50, 290, 113, 32))
self.start_butt.setObjectName("start_butt")
self.landuse_label = QtWidgets.QLabel(self.groupBox)
self.landuse_label.setGeometry(QtCore.QRect(20, 0, 201, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.landuse_label.setFont(font)
self.landuse_label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.landuse_label.setMidLineWidth(0)
self.landuse_label.setObjectName("landuse_label")
self.increase_label = QtWidgets.QLabel(self.groupBox)
self.increase_label.setGeometry(QtCore.QRect(20, 210, 201, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.increase_label.setFont(font)
self.increase_label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.increase_label.setMidLineWidth(0)
self.increase_label.setObjectName("increase_label")
self.rainfall_value = QtWidgets.QDoubleSpinBox(self.groupBox)
self.rainfall_value.setGeometry(QtCore.QRect(50, 240, 110, 24))
self.rainfall_value.setDecimals(1)
self.rainfall_value.setSingleStep(1.0)
self.rainfall_value.setObjectName("rainfall_value")
self.layoutWidget_9 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_9.setGeometry(QtCore.QRect(0, 170, 234, 26))
self.layoutWidget_9.setObjectName("layoutWidget_9")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.layoutWidget_9)
self.horizontalLayout_6.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
# Here is the code for the LCD Number
# -----------------------------------------------------------
self.total_label = QtWidgets.QLabel(self.layoutWidget_9)
font = QtGui.QFont()
font.setPointSize(12)
self.total_label.setFont(font)
self.total_label.setObjectName("total_label")
self.horizontalLayout_6.addWidget(self.total_label)
self.total_sum = QtWidgets.QLCDNumber(self.layoutWidget_9)
font = QtGui.QFont()
font.setPointSize(10)
self.total_sum.setFont(font)
self.total_sum.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
self.total_sum.setProperty("value", 0.0)
self.total_sum.setObjectName("total_sum")
self.horizontalLayout_6.addWidget(self.total_sum)
# -----------------------------------------------------------
self.layoutWidget_10 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_10.setGeometry(QtCore.QRect(0, 20, 234, 26))
self.layoutWidget_10.setObjectName("layoutWidget_10")
self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.layoutWidget_10)
self.horizontalLayout_7.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.urban_label = QtWidgets.QLabel(self.layoutWidget_10)
font = QtGui.QFont()
font.setPointSize(12)
self.urban_label.setFont(font)
self.urban_label.setObjectName("urban_label")
self.horizontalLayout_7.addWidget(self.urban_label)
self.urban_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_10)
self.urban_value.setSingleStep(0.05)
self.urban_value.setObjectName("urban_value")
self.horizontalLayout_7.addWidget(self.urban_value)
self.powered_by_label = QtWidgets.QLabel(self.tab)
self.powered_by_label.setGeometry(QtCore.QRect(460, 570, 111, 16))
font = QtGui.QFont()
font.setPointSize(11)
self.powered_by_label.setFont(font)
self.powered_by_label.setObjectName("powered_by_label")
self.landuse_label_2 = QtWidgets.QLabel(self.tab)
self.landuse_label_2.setGeometry(QtCore.QRect(330, 10, 421, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.landuse_label_2.setFont(font)
self.landuse_label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
self.landuse_label_2.setMidLineWidth(0)
self.landuse_label_2.setObjectName("landuse_label_2")
self.hydrograph_widget = QtWidgets.QGroupBox(self.tab)
self.hydrograph_widget.setGeometry(QtCore.QRect(280, 60, 711, 501))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.hydrograph_widget.setFont(font)
self.hydrograph_widget.setObjectName("hydrograph_widget")
self.plot_widget = QtWidgets.QWidget(self.hydrograph_widget)
self.plot_widget.setGeometry(QtCore.QRect(10, 30, 691, 461))
self.plot_widget.setObjectName("plot_widget")
self.tabWidget.addTab(self.tab, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1039, 22))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setCheckable(False)
self.actionClose.setObjectName("actionClose")
self.menuMenu.addAction(self.actionClose)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.urban_value, self.grass_value)
MainWindow.setTabOrder(self.grass_value, self.arab_value)
MainWindow.setTabOrder(self.arab_value, self.wood_value)
MainWindow.setTabOrder(self.wood_value, self.mount_value)
MainWindow.setTabOrder(self.mount_value, self.rainfall_value)
MainWindow.setTabOrder(self.rainfall_value, self.start_butt)
MainWindow.setTabOrder(self.start_butt, self.tabWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.grass_label.setText(_translate("MainWindow", "Grassland"))
self.arable_label.setText(_translate("MainWindow", "Arable"))
self.woodl_label.setText(_translate("MainWindow", "Woodland"))
self.mount_label.setText(_translate("MainWindow", "Mountain"))
self.start_butt.setText(_translate("MainWindow", "Start"))
self.landuse_label.setText(_translate("MainWindow", "Land Use Coverage (%)"))
self.increase_label.setText(_translate("MainWindow", "Increase in Rainfall (%)"))
self.total_label.setText(_translate("MainWindow", "Total"))
self.urban_label.setText(_translate("MainWindow", "Urban"))
self.powered_by_label.setText(_translate("MainWindow", "Powered by Group 1"))
self.landuse_label_2.setText(_translate("MainWindow", "Hydrological Modelling for the Ouseburn Catchment"))
self.hydrograph_widget.setTitle(_translate("MainWindow", "Hydrograph"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "HydroModel v.1.0"))
self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
self.actionClose.setText(_translate("MainWindow", "Close"))
#---------------------------------------------------------------------
# Here is my code for updating the LCDNumber
def all_inputs(self):
urban_inp = self.urban_value.value()
grass_inp = self.grass_value.value()
arab_inp = self.arab_value.value()
wood_inp = self.wood_value.value()
mount_inp = self.mount_value.value()
total = urban_inp + grass_inp + arab_inp + wood_inp + mount_inp
self.total_sum.display(total)
#---------------------------------------------------------------------
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_())
QDoubleSpinBox::valueChanged(double d)
This signal is emitted whenever the spin box's value is changed.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1039, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 0, 1001, 621))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.groupBox = QtWidgets.QGroupBox(self.tab)
self.groupBox.setGeometry(QtCore.QRect(10, 40, 241, 521))
font = QtGui.QFont()
font.setPointSize(11)
self.groupBox.setFont(font)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.layoutWidget_5 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_5.setGeometry(QtCore.QRect(0, 50, 234, 26))
self.layoutWidget_5.setObjectName("layoutWidget_5")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget_5)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.grass_label = QtWidgets.QLabel(self.layoutWidget_5)
font = QtGui.QFont()
font.setPointSize(12)
self.grass_label.setFont(font)
self.grass_label.setObjectName("grass_label")
self.horizontalLayout_2.addWidget(self.grass_label)
self.grass_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_5)
self.grass_value.setSingleStep(0.05)
self.grass_value.setObjectName("grass_value")
self.horizontalLayout_2.addWidget(self.grass_value)
self.layoutWidget_6 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_6.setGeometry(QtCore.QRect(0, 80, 234, 26))
self.layoutWidget_6.setObjectName("layoutWidget_6")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget_6)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.arable_label = QtWidgets.QLabel(self.layoutWidget_6)
font = QtGui.QFont()
font.setPointSize(12)
self.arable_label.setFont(font)
self.arable_label.setObjectName("arable_label")
self.horizontalLayout_3.addWidget(self.arable_label)
self.arab_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_6)
self.arab_value.setSingleStep(0.05)
self.arab_value.setObjectName("arab_value")
self.horizontalLayout_3.addWidget(self.arab_value)
self.layoutWidget_7 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_7.setGeometry(QtCore.QRect(0, 110, 234, 26))
self.layoutWidget_7.setObjectName("layoutWidget_7")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget_7)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.woodl_label = QtWidgets.QLabel(self.layoutWidget_7)
font = QtGui.QFont()
font.setPointSize(12)
self.woodl_label.setFont(font)
self.woodl_label.setObjectName("woodl_label")
self.horizontalLayout_4.addWidget(self.woodl_label)
self.wood_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_7)
self.wood_value.setSingleStep(0.05)
self.wood_value.setObjectName("wood_value")
self.horizontalLayout_4.addWidget(self.wood_value)
self.layoutWidget_8 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_8.setGeometry(QtCore.QRect(0, 140, 234, 26))
self.layoutWidget_8.setObjectName("layoutWidget_8")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.layoutWidget_8)
self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.mount_label = QtWidgets.QLabel(self.layoutWidget_8)
font = QtGui.QFont()
font.setPointSize(12)
self.mount_label.setFont(font)
self.mount_label.setObjectName("mount_label")
self.horizontalLayout_5.addWidget(self.mount_label)
self.mount_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_8)
self.mount_value.setSingleStep(0.05)
self.mount_value.setObjectName("mount_value")
self.horizontalLayout_5.addWidget(self.mount_value)
self.start_butt = QtWidgets.QPushButton(self.groupBox)
self.start_butt.setGeometry(QtCore.QRect(50, 290, 113, 32))
self.start_butt.setObjectName("start_butt")
self.landuse_label = QtWidgets.QLabel(self.groupBox)
self.landuse_label.setGeometry(QtCore.QRect(20, 0, 201, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.landuse_label.setFont(font)
self.landuse_label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.landuse_label.setMidLineWidth(0)
self.landuse_label.setObjectName("landuse_label")
self.increase_label = QtWidgets.QLabel(self.groupBox)
self.increase_label.setGeometry(QtCore.QRect(20, 210, 201, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.increase_label.setFont(font)
self.increase_label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.increase_label.setMidLineWidth(0)
self.increase_label.setObjectName("increase_label")
self.rainfall_value = QtWidgets.QDoubleSpinBox(self.groupBox)
self.rainfall_value.setGeometry(QtCore.QRect(50, 240, 110, 24))
self.rainfall_value.setDecimals(1)
self.rainfall_value.setSingleStep(1.0)
self.rainfall_value.setObjectName("rainfall_value")
self.layoutWidget_9 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_9.setGeometry(QtCore.QRect(0, 170, 234, 26))
self.layoutWidget_9.setObjectName("layoutWidget_9")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.layoutWidget_9)
self.horizontalLayout_6.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
# Here is the code for the LCD Number
# -----------------------------------------------------------
self.total_label = QtWidgets.QLabel(self.layoutWidget_9)
font = QtGui.QFont()
font.setPointSize(12)
self.total_label.setFont(font)
self.total_label.setObjectName("total_label")
self.horizontalLayout_6.addWidget(self.total_label)
self.total_sum = QtWidgets.QLCDNumber(self.layoutWidget_9)
font = QtGui.QFont()
font.setPointSize(10)
self.total_sum.setFont(font)
self.total_sum.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
self.total_sum.setProperty("value", 0.0)
self.total_sum.setObjectName("total_sum")
self.horizontalLayout_6.addWidget(self.total_sum)
# -----------------------------------------------------------
self.layoutWidget_10 = QtWidgets.QWidget(self.groupBox)
self.layoutWidget_10.setGeometry(QtCore.QRect(0, 20, 234, 26))
self.layoutWidget_10.setObjectName("layoutWidget_10")
self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.layoutWidget_10)
self.horizontalLayout_7.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.urban_label = QtWidgets.QLabel(self.layoutWidget_10)
font = QtGui.QFont()
font.setPointSize(12)
self.urban_label.setFont(font)
self.urban_label.setObjectName("urban_label")
self.horizontalLayout_7.addWidget(self.urban_label)
self.urban_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_10)
self.urban_value.setSingleStep(0.05)
self.urban_value.setObjectName("urban_value")
self.horizontalLayout_7.addWidget(self.urban_value)
self.powered_by_label = QtWidgets.QLabel(self.tab)
self.powered_by_label.setGeometry(QtCore.QRect(460, 570, 111, 16))
font = QtGui.QFont()
font.setPointSize(11)
self.powered_by_label.setFont(font)
self.powered_by_label.setObjectName("powered_by_label")
self.landuse_label_2 = QtWidgets.QLabel(self.tab)
self.landuse_label_2.setGeometry(QtCore.QRect(330, 10, 421, 21))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
self.landuse_label_2.setFont(font)
self.landuse_label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
self.landuse_label_2.setMidLineWidth(0)
self.landuse_label_2.setObjectName("landuse_label_2")
self.hydrograph_widget = QtWidgets.QGroupBox(self.tab)
self.hydrograph_widget.setGeometry(QtCore.QRect(280, 60, 711, 501))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.hydrograph_widget.setFont(font)
self.hydrograph_widget.setObjectName("hydrograph_widget")
self.plot_widget = QtWidgets.QWidget(self.hydrograph_widget)
self.plot_widget.setGeometry(QtCore.QRect(10, 30, 691, 461))
self.plot_widget.setObjectName("plot_widget")
self.tabWidget.addTab(self.tab, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1039, 22))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setCheckable(False)
self.actionClose.setObjectName("actionClose")
self.menuMenu.addAction(self.actionClose)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.urban_value, self.grass_value)
MainWindow.setTabOrder(self.grass_value, self.arab_value)
MainWindow.setTabOrder(self.arab_value, self.wood_value)
MainWindow.setTabOrder(self.wood_value, self.mount_value)
MainWindow.setTabOrder(self.mount_value, self.rainfall_value)
MainWindow.setTabOrder(self.rainfall_value, self.start_butt)
MainWindow.setTabOrder(self.start_butt, self.tabWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.grass_label.setText(_translate("MainWindow", "Grassland"))
self.arable_label.setText(_translate("MainWindow", "Arable"))
self.woodl_label.setText(_translate("MainWindow", "Woodland"))
self.mount_label.setText(_translate("MainWindow", "Mountain"))
self.start_butt.setText(_translate("MainWindow", "Start"))
self.landuse_label.setText(_translate("MainWindow", "Land Use Coverage (%)"))
self.increase_label.setText(_translate("MainWindow", "Increase in Rainfall (%)"))
self.total_label.setText(_translate("MainWindow", "Total"))
self.urban_label.setText(_translate("MainWindow", "Urban"))
self.powered_by_label.setText(_translate("MainWindow", "Powered by Group 1"))
self.landuse_label_2.setText(_translate("MainWindow", "Hydrological Modelling for the Ouseburn Catchment"))
self.hydrograph_widget.setTitle(_translate("MainWindow", "Hydrograph"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "HydroModel v.1.0"))
self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
self.actionClose.setText(_translate("MainWindow", "Close"))
class Example(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(Example, self).__init__()
self.setupUi(self)
self.urban_value.valueChanged.connect(self.all_inputs) # +
self.grass_value.valueChanged.connect(self.all_inputs) # +
self.arab_value.valueChanged.connect(self.all_inputs) # +
self.wood_value.valueChanged.connect(self.all_inputs) # +
self.mount_value.valueChanged.connect(self.all_inputs) # +
# Here is my code for updating the LCDNumber
def all_inputs(self):
urban_inp = self.urban_value.value()
grass_inp = self.grass_value.value()
arab_inp = self.arab_value.value()
wood_inp = self.wood_value.value()
mount_inp = self.mount_value.value()
total = urban_inp + grass_inp + arab_inp + wood_inp + mount_inp
self.total_sum.display(total)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
Extracting data from MySQL (name ,sex, age, city ) (sex=1 or 2 , age = 1 to 6 agegroup and cite by code). Now I want concatenate sex, age and city codes with (day month year) into barcoding column (anther tablewidget_2) according appearing in table widget_1 and result for example should be like this 132612052019 (sex code+age code+city code +date).
from PyQt5 import QtCore, QtGui, QtWidgets
import pymysql
import random
class Ui_MainWindow(object):
def __init__(self):
self.conn = pymysql.connect(host="localhost", user="root", password="bdr654321", database="badr")
self.mycur = self.conn.cursor()
def loaddata(self):
sex = self.lineEdit.text()
age = self.lineEdit_2.text()
city = self.lineEdit_3.text()
result = self.mycur.execute("SELECT * FROM example WHERE age = '%s' AND city = '%s' " % (''.join(age), ''.join(city)))
res = self.mycur.fetchall()
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(res):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
return
self.conn.close()
def showmessagebox(self, title, message):
mesgbox = QtGui.QMessageBox()
mesgbox.seticon(QtGui.QMessageBox.Warning)
mesgbox.setWindowTitle(title)
mesgbox.setText(message)
mesgbox.setStandardButtons(QtGui.QMessageBox.OK)
mesgbox.exec()
def search(self):
name_1 = self.lineEdit_5.text()
result_1 = self.mycur.execute("SELECT * FROM example WHERE name='%s' " % (''.join(name_1)))
res_1 = self.mycur.fetchall()
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(res_1):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
return
self.conn.close()
def randdata(self):
rand_1 = self.lineEdit_4.text()
age = self.lineEdit_2.text()
city = self.lineEdit_3.text()
result_2 = self.mycur.execute( "SELECT * FROM example WHERE age = '%s' AND city = '%s' " % (''.join(age), ''.join(city)))
res_2 = self.mycur.fetchall()
random_2 = random.sample(res_2, k=int(rand_1))
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(random_2):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
return
self.conn.close()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1312, 852)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(30, 150, 821, 401))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(7)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(6, item)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 80, 171, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(210, 80, 101, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(320, 80, 113, 41))
self.lineEdit_3.setObjectName("lineEdit_3")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(70, 50, 68, 19))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(210, 50, 68, 19))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(330, 50, 68, 19))
self.label_3.setObjectName("label_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(440, 80, 113, 41))
self.lineEdit_4.setObjectName("lineEdit_4")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(440, 18, 191, 51))
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 10, 121, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.loaddata)
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(757, 40, 161, 41))
self.label_5.setObjectName("label_5")
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setGeometry(QtCore.QRect(750, 80, 181, 41))
self.lineEdit_5.setObjectName("lineEdit_5")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(80, 590, 161, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(290, 590, 211, 41))
self.pushButton_3.setObjectName("pushButton_3")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(860, 150, 191, 401))
self.listWidget.setObjectName("listWidget")
self.tableWidget_2 = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_2.setGeometry(QtCore.QRect(1060, 150, 256, 401))
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.setColumnCount(1)
self.tableWidget_2.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget_2.setHorizontalHeaderItem(0, item)
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(520, 600, 761, 192))
self.graphicsView.setObjectName("graphicsView")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(30, 650, 448, 289))
self.calendarWidget.setObjectName("calendarWidget")
self.dateEdit = QtWidgets.QDateEdit(self.centralwidget)
self.dateEdit.setGeometry(QtCore.QRect(1030, 30, 171, 41))
self.dateEdit.setObjectName("dateEdit")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(640, 560, 112, 34))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(820, 560, 112, 34))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(940, 80, 112, 34))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_6.clicked.connect(self.search)
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(560, 80, 112, 34))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_7.clicked.connect(self.randdata)
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", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Sex"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Age"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "City"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "barcode(of item)"))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "lat"))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "long"))
self.label.setText(_translate("MainWindow", "Sex"))
self.label_2.setText(_translate("MainWindow", "Age"))
self.label_3.setText(_translate("MainWindow", "City"))
self.label_4.setText(_translate("MainWindow", "No of sample random"))
self.pushButton.setText(_translate("MainWindow", "Run"))
self.label_5.setText(_translate("MainWindow", "Search by name"))
self.pushButton_2.setText(_translate("MainWindow", "print barcode"))
self.pushButton_3.setText(_translate("MainWindow", "export file(cvs file)"))
item = self.tableWidget_2.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "barcodeing"))
self.pushButton_4.setText(_translate("MainWindow", "map"))
self.pushButton_5.setText(_translate("MainWindow", "edit data"))
self.pushButton_6.setText(_translate("MainWindow", "search"))
self.pushButton_7.setText(_translate("MainWindow", "random"))
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_())
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_())