How to get value from dialog button to mainwindow tablewidget in pyqt5? - python

How can I get value from dialog button to mainwindow?
How can if the button on my dialog is pressed, give value to table widget on mainwindow?
Here my mainwindow code and dialog code, and code what I want to impact each other.
mainwindow Code
from PyQt5 import QtCore, QtGui, QtWidgets
from ayam_ui import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(330, 0, 121, 41))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(20)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 100, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(210, 100, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(300, 100, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(390, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(550, 100, 231, 341))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(10)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menumenu = QtWidgets.QMenu(self.menubar)
self.menumenu.setObjectName("menumenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menumenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ayam)
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText("ayam")
def ayam(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Kantin"))
self.label.setText(_translate("MainWindow", "Makanan"))
self.pushButton.setText(_translate("MainWindow", "Ayam"))
self.pushButton_2.setText(_translate("MainWindow", "Mie"))
self.pushButton_3.setText(_translate("MainWindow", "Sayur"))
self.pushButton_4.setText(_translate("MainWindow", "Daging"))
self.pushButton_5.setText(_translate("MainWindow", "Ikan"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText (_translate("mainwindows", "1"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Makanan"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "jumlah"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.menumenu.setTitle(_translate("MainWindow", "menu"))
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_())
and this the dialog
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(348, 496)
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(0, 280, 201, 201))
self.textBrowser.setObjectName("textBrowser")
self.pesan = QtWidgets.QPushButton(Dialog)
self.pesan.setGeometry(QtCore.QRect(210, 300, 75, 23))
self.pesan.setObjectName("pesan")
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(300, 300, 42, 22))
self.spinBox.setObjectName("spinBox")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(210, 350, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(0, -10, 1351, 291))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("E:/python/resto/ayy.jpg"))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton_2.clicked.connect(Dialog.close)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.textBrowser.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">AYAM</span></p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:12pt; font-weight:600; text-decoration: underline;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">ini adalah ayam yang di goreng dengan minyak</span></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">enak gurih dan renyah</span></p></body></html>"))
self.pesan.setText(_translate("Dialog", "pesan"))
self.pushButton_2.setText(_translate("Dialog", "kembali"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
How can this button on the dialog
self.pesan = QtWidgets.QPushButton(Dialog)
self.pesan.setGeometry(QtCore.QRect(210, 300, 75, 23))
self.pesan.setObjectName("pesan")
impact to this defintion on my main window?
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText("ayam")

Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
from ayam_ui import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(330, 0, 121, 41))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(20)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 100, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(210, 100, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(300, 100, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(390, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(550, 100, 231, 341))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(10)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menumenu = QtWidgets.QMenu(self.menubar)
self.menumenu.setObjectName("menumenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menumenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ayam)
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText(str(self.ui.spinBox.value())) #("ayam") # +++
self.Dialog.hide() # +++
def ayam(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.ui.pesan.clicked.connect(self.pesanayam) # +++
self.Dialog.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Kantin"))
self.label.setText(_translate("MainWindow", "Makanan"))
self.pushButton.setText(_translate("MainWindow", "Ayam"))
self.pushButton_2.setText(_translate("MainWindow", "Mie"))
self.pushButton_3.setText(_translate("MainWindow", "Sayur"))
self.pushButton_4.setText(_translate("MainWindow", "Daging"))
self.pushButton_5.setText(_translate("MainWindow", "Ikan"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText (_translate("mainwindows", "1"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Makanan"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "jumlah"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.menumenu.setTitle(_translate("MainWindow", "menu"))
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_())

Related

name 'QTableWidgetItem' is not defined

I tried install all rquired libsI tried install all rquired libs and Im so sorry I am new here,Im trying to show datas in database on that widget and this problem occurs,I searched all others about that but could not find any issue
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtCore, QtGui, QtWidgets
import mysql.connector
from PyQt5.QtWidgets import QDialog , QApplication , QLineEdit , QInputDialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(60, 0, 721, 300))
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(60, 300, 133, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(60, 320, 133, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(60, 340, 133, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(60, 360, 133, 20))
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setGeometry(QtCore.QRect(60, 380, 133, 20))
self.lineEdit_5.setObjectName("lineEdit_5")
self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_6.setGeometry(QtCore.QRect(60, 400, 133, 20))
self.lineEdit_6.setObjectName("lineEdit_6")
self.pushButton = QtWidgets.QPushButton(self.centralwidget,clicked=lambda:self.save())
self.pushButton.setGeometry(QtCore.QRect(193, 300, 91, 121))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(290, 300, 47, 20))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 320, 47, 20))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(290, 340, 47, 20))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(290, 360, 47, 20))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(290, 380, 47, 20))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(290, 400, 47, 20))
self.label_6.setObjectName("label_6")
self.lineEdit_7 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_7.setGeometry(QtCore.QRect(330, 320, 133, 20))
self.lineEdit_7.setObjectName("lineEdit_7")
self.lineEdit_8 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_8.setGeometry(QtCore.QRect(330, 360, 133, 20))
self.lineEdit_8.setObjectName("lineEdit_8")
self.lineEdit_9 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_9.setGeometry(QtCore.QRect(330, 380, 133, 20))
self.lineEdit_9.setObjectName("lineEdit_9")
self.lineEdit_10 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_10.setGeometry(QtCore.QRect(330, 400, 133, 20))
self.lineEdit_10.setObjectName("lineEdit_10")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget,clicked=lambda:self.edit())
self.pushButton_2.setGeometry(QtCore.QRect(460, 300, 91, 121))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "sayı"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "merkez"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "sayac no"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "geliş"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "çıkış"))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "fiyat-geliş"))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "fiyat-çıkış"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "MERKEZ"))
self.label_2.setText(_translate("MainWindow", "SAYAC"))
self.label_3.setText(_translate("MainWindow", "GELİŞ"))
self.label_4.setText(_translate("MainWindow", "ÇIKIŞ"))
self.label_5.setText(_translate("MainWindow", "GELİŞ-F"))
self.label_6.setText(_translate("MainWindow", "ÇIKIŞ-F"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
def save(self):
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="Tu41893982790",
database="deneme")
mycursor=mydb.cursor()
sql="INSERT INTO sayac(merkez,sayac_no,gelis_tarihi,cikis_tarihi,gelis_fiyati,cikis_fiyati) VALUES(%s,%s,%s,%s,%s,%s)"
val=(str(self.lineEdit.text()),str(self.lineEdit_2.text()),str(self.lineEdit_3.text()),str(self.lineEdit_4.text()),str(self.lineEdit_5.text()),str(self.lineEdit_6.text()))
mycursor.execute(sql,val)
mydb.commit()
def edit(self):
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="Tu41893982790",
database="deneme")
mycursor=mydb.cursor()
sql=f"UPDATE sayac SET cikis_tarihi={self.lineEdit_8.text()},gelis_fiyati={self.lineEdit_9.text()},cikis_fiyati={self.lineEdit_10.text()} WHERE sayac_no={self.lineEdit_7.text()}"
mycursor.execute(sql)
mydb.commit()
def load_data(self):
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="Tu41893982790",
database="deneme")
mycursor=mydb.cursor()
sql="SELECT * FROM sayac"
mycursor.execute(sql)
results=mycursor.fetchall()
results.reverse()
results=list(results)
row=0
for sayac in results:
self.tableWidget.setItem(row,0,QtWidgets,QTableWidgetItem(sayac[0]))
self.tableWidget.setItem(row,1,QtWidgets,QTableWidgetItem(sayac[1]))
self.tableWidget.setItem(row,2,QtWidgets,QTableWidgetItem(sayac[2]))
self.tableWidget.setItem(row,3,QtWidgets,QTableWidgetItem(sayac[3]))
self.tableWidget.setItem(row,4,QtWidgets,QTableWidgetItem(sayac[4]))
self.tableWidget.setItem(row,5,QtWidgets,QTableWidgetItem(sayac[5]))
self.tableWidget.setItem(row,6,QtWidgets,QTableWidgetItem(sayac[6]))
row+=1
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 have noticed that this problem occurs because of 'for sayac in results:' when it is removed,no problem but ı need this loop

pyqt5 button click event works in debug but not when runing

i have a main code that runs a main UI window, from the main UI window im passing to other window, i tried to implement a back button but for some reason it works only when im in debug mode.
main UI window:
from PyQt5 import QtCore, QtGui, QtWidgets
import UI.plantManagment as plantManagment
class Ui_MainWindow(object):
def __init__(self,MainWindow,loadedgreenhouses) -> None:
self.MainWindow=MainWindow
self.loadedgreenhouses=loadedgreenhouses
def setupUi(self):
self.MainWindow.setObjectName("MainWindow")
self.MainWindow.resize(480, 480)
self.MainWindow.setMinimumSize(QtCore.QSize(480, 480))
self.MainWindow.setMaximumSize(QtCore.QSize(480, 480))
self.MainWindow.setTabletTracking(False)
self.MainWindow.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.MainWindow.setAnimated(True)
self.MainWindow.setDocumentMode(False)
self.MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(self.MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(70, 70, 321, 361))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.btn_managePlants = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.btn_managePlants.setMinimumSize(QtCore.QSize(0, 80))
font = QtGui.QFont()
font.setPointSize(24)
self.btn_managePlants.setFont(font)
self.btn_managePlants.setObjectName("btn_managePlants")
self.verticalLayout.addWidget(self.btn_managePlants)
self.btn_managePlants.clicked.connect(self.loadPalntsUI)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.btn_manageGreenHouses = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.btn_manageGreenHouses.setMinimumSize(QtCore.QSize(0, 80))
font = QtGui.QFont()
font.setPointSize(24)
self.btn_manageGreenHouses.setFont(font)
self.btn_manageGreenHouses.setObjectName("btn_manageGreenHouses")
self.verticalLayout.addWidget(self.btn_manageGreenHouses)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
self.btn_harvestAll = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.btn_harvestAll.setMinimumSize(QtCore.QSize(0, 80))
font = QtGui.QFont()
font.setPointSize(24)
self.btn_harvestAll.setFont(font)
self.btn_harvestAll.setCheckable(False)
self.btn_harvestAll.setChecked(False)
self.btn_harvestAll.setObjectName("btn_harvestAll")
self.verticalLayout.addWidget(self.btn_harvestAll)
spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem3)
self.lable_harvestableNum = QtWidgets.QLabel(self.centralwidget)
self.lable_harvestableNum.setGeometry(QtCore.QRect(70, 20, 321, 41))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(False)
self.lable_harvestableNum.setFont(font)
self.lable_harvestableNum.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lable_harvestableNum.setScaledContents(False)
self.lable_harvestableNum.setAlignment(QtCore.Qt.AlignCenter)
self.lable_harvestableNum.setWordWrap(False)
self.lable_harvestableNum.setIndent(2)
self.lable_harvestableNum.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)
self.lable_harvestableNum.setObjectName("lable_harvestableNum")
self.MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(self.MainWindow)
self.statusbar.setObjectName("statusbar")
self.MainWindow.setStatusBar(self.statusbar)
self.retranslateUi()
QtCore.QMetaObject.connectSlotsByName(self.MainWindow)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btn_managePlants.setText(_translate("MainWindow", "manage plants"))
self.btn_manageGreenHouses.setText(_translate("MainWindow", "manage greenhouses"))
self.btn_harvestAll.setText(_translate("MainWindow", "harvest all"))
self.lable_harvestableNum.setText(_translate("MainWindow", f"plant ready for harvest: {self.calcRadyToHarvest()}"))
def calcRadyToHarvest(self):
cnt=0
for gh in self.loadedgreenhouses:
for pp in gh.palntingPoints:
if pp.harvestPlant():
cnt+=1
return cnt
def loadPalntsUI(self):
ui = plantManagment.Ui_MainWindow(self.MainWindow,self.loadedgreenhouses)
ui.setupUi()
ui.MainWindow.show()
here the event is working as intended when i click on self.btn_managePlants.clicked.connect(self.loadPalntsUI) and it loads me the next window witouth a problem.
the second window (where the button doesn't work):
from time import sleep
from PyQt5 import QtCore, QtGui, QtWidgets
import Plant
import UI.mainWindow as mainWindow
class Ui_MainWindow(object):
def __init__(self,MainWindow,loadedgreenhouses) -> None:
self.MainWindow=MainWindow
self.loadedgreenhouses=loadedgreenhouses
def setupUi(self):
self.MainWindow.setObjectName("MainWindow")
self.MainWindow.setWindowModality(QtCore.Qt.NonModal)
self.MainWindow.setEnabled(True)
self.MainWindow.resize(627, 409)
self.MainWindow.setMinimumSize(QtCore.QSize(627, 409))
self.MainWindow.setMaximumSize(QtCore.QSize(627, 409))
self.MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(self.MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 591, 361))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.label_plants = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(18)
self.label_plants.setFont(font)
self.label_plants.setObjectName("label_plants")
self.verticalLayout.addWidget(self.label_plants)
self.tableWidget = QtWidgets.QTableWidget(self.verticalLayoutWidget)
self.tableWidget.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
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)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.verticalHeader().setHighlightSections(False)
self.verticalLayout.addWidget(self.tableWidget)
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) #forbid editing
self.tableWidget.setRowCount(len(Plant.Plant.kindsList)) #len of number of plants
for i,p in enumerate(Plant.Plant.plantsLst): #add to the table
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(p.kind))
self.tableWidget.setItem(i, 1, QtWidgets.QTableWidgetItem(str(p.grow_time)))
self.tableWidget.setItem(i, 2, QtWidgets.QTableWidgetItem(str(p.temp_min)))
self.tableWidget.setItem(i, 3, QtWidgets.QTableWidgetItem(str(p.temp_max)))
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.btn_back = QtWidgets.QPushButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.btn_back.setFont(font)
self.btn_back.setObjectName("btn_back")
self.horizontalLayout.addWidget(self.btn_back)
self.btn_back.clicked.connect(self.backBtn)
self.btn_delete = QtWidgets.QPushButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.btn_delete.setFont(font)
self.btn_delete.setObjectName("btn_delete")
self.horizontalLayout.addWidget(self.btn_delete)
self.btn_addPlant = QtWidgets.QPushButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.btn_addPlant.setFont(font)
self.btn_addPlant.setObjectName("btn_addPlant")
self.horizontalLayout.addWidget(self.btn_addPlant)
self.verticalLayout.addLayout(self.horizontalLayout)
self.MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(self.MainWindow)
self.statusbar.setObjectName("statusbar")
self.MainWindow.setStatusBar(self.statusbar)
self.retranslateUi()
QtCore.QMetaObject.connectSlotsByName(self.MainWindow)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.MainWindow.setWindowTitle(_translate("MainWindow", "plant managment"))
self.label_plants.setText(_translate("MainWindow", "plants:"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "kind"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "grow time"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "min temperature"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "max temperature"))
self.btn_back.setText(_translate("MainWindow", "back"))
self.btn_delete.setText(_translate("MainWindow", "delete"))
self.btn_addPlant.setText(_translate("MainWindow", "add plant"))
self.tableWidget.show()
def backBtn(self):
print("the lalala")
ui = mainWindow.Ui_MainWindow(self.MainWindow,self.loadedgreenhouses)
ui.setupUi()
self.MainWindow.show()
the event should be triggered by self.btn_back.clicked.connect(self.backBtn) but as i said it would work only in debug mode.
i tried to self.btn_back.clicked.connect(lambda : self.backBtn()) which did took me back to the main UI window but then none of the buttons in that window worked :|

Change main window active tab from a dialogbox action

I have two windows in 2 separate classes. The main window and a dialogbox window. The main window contains a Qtabwidget with multiple tabs. The dialogbox has a button.
From the main window, on the click of a button the dialogbox opens. The user performs some action and clicks the save button. On the click of the button I would like the dialogbox to close, and the mainwindow to change from the initial tab to the second tab.
My problem is that I cannot get the tabwidget to change tabs on the dialogbox save button click signal.
Attempt:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 138)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
self.verticalLayout.addWidget(self.lineEdit_vehicleName)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton_save = QtWidgets.QPushButton(self.frame)
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
self.pushButton_cancel.setObjectName("pushButton_cancel")
self.horizontalLayout.addWidget(self.pushButton_cancel)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# Dialog = QtWidgets.QDialog()
self.pushButton_save.clicked.connect(Dialog.close)
self.pushButton_save.clicked.connect(Ui_MainWindow.changeTab)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Enter Name:"))
self.pushButton_save.setText(_translate("Dialog", "Save"))
self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
class Ui_MainWindow(object):
def changeTab(self):
self.tabWidget.setCurrentIndex(1)
def openDialog(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(548, 399)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.tabWidget.setCurrentIndex(0)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.openDialog)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
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_())
PyQt recommends not modifying the code generated by Qt Designer, instead it creates a class that inherits from the appropriate widget. On the other hand, the save and cancel buttons must be connected to the accept and reject methods, respectively. To get the information you can use exec_() that will return a value associated to the button that you have pressed, and if it is the save button then you must change the currentIndex of QTabWidget
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 138)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
self.verticalLayout.addWidget(self.lineEdit_vehicleName)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton_save = QtWidgets.QPushButton(self.frame)
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
self.pushButton_cancel.setObjectName("pushButton_cancel")
self.horizontalLayout.addWidget(self.pushButton_cancel)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Enter Name:"))
self.pushButton_save.setText(_translate("Dialog", "Save"))
self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(548, 399)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.tabWidget.setCurrentIndex(0)
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", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
#QtCore.pyqtSlot()
def openDialog(self):
self.dialog = Dialog()
if self.dialog.exec_() == QtWidgets.QDialog.Accepted:
self.tabWidget.setCurrentIndex(1)
class Dialog(Ui_Dialog, QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.pushButton_save.clicked.connect(self.accept)
self.pushButton_cancel.clicked.connect(self.reject)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
For more information read https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html

Is there a way to change a label of one window from another window?

I've got some code that checks which checkboxes a user clicks. Those are then put into an array (aylm). Once the user clicks the book button, a small confirmation window pops up which says are you sure you want to book these rooms. In this confirmation window, I would like the label to say are you sure you want to book (aylm)? so that all the rooms and times selected can be seen by the user. I am not sure how to go about this since I'm using one main window which connects all the smaller windows together, which you can see below in the code.
class Ui_DialogBook(QtWidgets.QDialog, Ui_DialogBook):
def __init__(self, parent=None):
super(Ui_DialogBook, self).__init__(parent)
self.setupUi(self)
class Ui_ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
def __init__(self, parent=None):
super(Ui_ViewAllRooms, self).__init__(parent)
self.setupUi(self)
self.dialogBook = Ui_DialogBook()
self.book_Button.clicked.connect(self.dialogBook.show)
self.book_Button.clicked.connect(self.book_clicked)
#QtCore.pyqtSlot()
def book_clicked(self):
items = []
for i in range(self.tableWidget.rowCount()):
for j in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(i, j)
if item.checkState() == QtCore.Qt.Checked:
items.append(item)
for it in items:
r = it.row()
c = it.column()
v, h = self.tableWidget.horizontalHeaderItem(c).text(), self.tableWidget.verticalHeaderItem(r).text()
self._checked_items.append([h, v])
aylm = (self.checked_items())
aylm_len = len(aylm)
for nm in range (0, aylm_len):
print (aylm[nm])
def checked_items(self):
return self._checked_items
The auto generated code from PyQt designer for the Ui_DialogBook is below:
class Ui_DialogBook(object):
def setupUi(self, DialogBook):
DialogBook.setObjectName("DialogBook")
DialogBook.setMaximumSize(QtCore.QSize(200, 150))
self.invalidLogin_label = QtWidgets.QLabel(DialogBook)
self.invalidLogin_label.setGeometry(QtCore.QRect(10, 10, 181, 71))
self.invalidLogin_label.setAlignment(QtCore.Qt.AlignCenter)
self.invalidLogin_label.setObjectName("invalidLogin_label")
self.no_button = QtWidgets.QPushButton(DialogBook)
self.no_button.setGeometry(QtCore.QRect(110, 100, 75, 23))
self.no_button.setObjectName("no_button")
self.yes_button = QtWidgets.QPushButton(DialogBook)
self.yes_button.setGeometry(QtCore.QRect(20, 100, 75, 23))
self.yes_button.setObjectName("yes_button")
self.retranslateUi(DialogBook)
QtCore.QMetaObject.connectSlotsByName(DialogBook)
def retranslateUi(self, DialogBook):
_translate = QtCore.QCoreApplication.translate
DialogBook.setWindowTitle(_translate("DialogBook", "Dialog"))
self.invalidLogin_label.setText(_translate("DialogBook", "Are you sure you would like \n to book:\n"))
self.no_button.setText(_translate("DialogBook", "No"))
self.yes_button.setText(_translate("DialogBook", "Yes"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DialogBook = QtWidgets.QDialog()
ui = Ui_DialogBook()
ui.setupUi(DialogBook)
DialogBook.show()
sys.exit(app.exec_())
Ui_ViewAllRooms
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
class Ui_ViewAllRooms(object):
def setupUi(self, ViewAllRooms):
ViewAllRooms.setObjectName("ViewAllRooms")
ViewAllRooms.resize(671, 347)
ViewAllRooms.setMinimumSize(QtCore.QSize(671, 347))
ViewAllRooms.setMaximumSize(QtCore.QSize(671, 347))
self.centralwidget = QtWidgets.QWidget(ViewAllRooms)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 100, 651, 181))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(6)
self.tableWidget.setRowCount(5)
self.tableWidget.setHorizontalHeaderLabels("P1 P2 P3 P4 P5 P6".split())
self.tableWidget.setVerticalHeaderLabels("C101 C214 C320 F04 E201".split())
for i in range(self.tableWidget.rowCount()):
for j in range(self.tableWidget.columnCount()):
item = QtWidgets.QTableWidgetItem()
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.tableWidget.setItem(i, j, item)
self._checked_items = []
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 290, 231, 41))
self.label.setObjectName("label")
self.book_Button = QtWidgets.QPushButton(self.centralwidget)
self.book_Button.setGeometry(QtCore.QRect(300, 290, 75, 23))
self.book_Button.setObjectName("book_Button")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(590, 10, 75, 23))
self.backButton.setObjectName("backButton")
self.login_title = QtWidgets.QLabel(self.centralwidget)
self.login_title.setGeometry(QtCore.QRect(220, 10, 231, 38))
font = QtGui.QFont()
font.setFamily("Gloucester MT Extra Condensed")
font.setPointSize(25)
font.setBold(False)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(50)
self.login_title.setFont(font)
self.login_title.setLayoutDirection(QtCore.Qt.LeftToRight)
self.login_title.setAutoFillBackground(False)
self.login_title.setAlignment(QtCore.Qt.AlignCenter)
self.login_title.setObjectName("login_title")
self.date_label = QtWidgets.QLabel(self.centralwidget)
self.date_label.setGeometry(QtCore.QRect(240, 70, 191, 21))
self.date_label.setAlignment(QtCore.Qt.AlignCenter)
self.date_label.setObjectName("date_label")
self.nextDayButton = QtWidgets.QPushButton(self.centralwidget)
self.nextDayButton.setGeometry(QtCore.QRect(590, 70, 75, 23))
self.nextDayButton.setObjectName("nextDayButton")
ViewAllRooms.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(ViewAllRooms)
self.statusbar.setObjectName("statusbar")
ViewAllRooms.setStatusBar(self.statusbar)
self.retranslateUi(ViewAllRooms)
QtCore.QMetaObject.connectSlotsByName(ViewAllRooms)
def retranslateUi(self, ViewAllRooms):
_translate = QtCore.QCoreApplication.translate
ViewAllRooms.setWindowTitle(_translate("ViewAllRooms", "MainWindow"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "C101"))
item.setToolTip("Hall")
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "C214"))
item.setToolTip("Study Space")
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "C320"))
item.setToolTip("Computer")
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "F04"))
item.setToolTip("Mac Room")
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "E201"))
item.setToolTip("Computer")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "P1"))
item.setToolTip("8:55")
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "P2"))
item.setToolTip("9:55")
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "P3"))
item.setToolTip("11:15")
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "P4"))
item.setToolTip("12:15")
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "P5"))
item.setToolTip("2:00")
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("ViewAllRooms", "P6"))
item.setToolTip("3:15")
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.book_Button.setText(_translate("ViewAllRooms", "Book"))
self.backButton.setText(_translate("ViewAllRooms", "Back"))
self.login_title.setText(_translate("ViewAllRooms", "VIEW ALL ROOMS"))
self.date_label.setText(_translate("ViewAllRooms", "Date:14 January 2019"))
self.label.setText(_translate("ViewAllRooms", "Hover over a room number or period for\n"
"extra information"))
self.nextDayButton.setText(_translate("ViewAllRooms", "Next Day"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ViewAllRooms = QtWidgets.QMainWindow()
ui = Ui_ViewAllRooms()
ui.setupUi(ViewAllRooms)
ViewAllRooms.show()
sys.exit(app.exec_())
View All Rooms GUI
Try it :
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui_DialogBook(object):
def setupUi(self, DialogBook):
DialogBook.setObjectName("DialogBook")
DialogBook.setMaximumSize(QtCore.QSize(200, 150))
self.invalidLogin_label = QtWidgets.QLabel(DialogBook)
self.invalidLogin_label.setGeometry(QtCore.QRect(10, 10, 181, 71))
self.invalidLogin_label.setAlignment(QtCore.Qt.AlignCenter)
self.invalidLogin_label.setObjectName("invalidLogin_label")
self.no_button = QtWidgets.QPushButton(DialogBook)
self.no_button.setGeometry(QtCore.QRect(110, 100, 75, 23))
self.no_button.setObjectName("no_button")
self.yes_button = QtWidgets.QPushButton(DialogBook)
self.yes_button.setGeometry(QtCore.QRect(20, 100, 75, 23))
self.yes_button.setObjectName("yes_button")
self.retranslateUi(DialogBook)
QtCore.QMetaObject.connectSlotsByName(DialogBook)
def retranslateUi(self, DialogBook):
_translate = QtCore.QCoreApplication.translate
DialogBook.setWindowTitle(_translate("DialogBook", "Dialog"))
self.invalidLogin_label.setText(_translate("DialogBook", "Are you sure you would like \n to book:\n"))
self.no_button.setText(_translate("DialogBook", "No"))
self.yes_button.setText(_translate("DialogBook", "Yes"))
class DialogBook(QtWidgets.QDialog, Ui_DialogBook):
def __init__(self, parent=None):
super(DialogBook, self).__init__(parent)
self.setupUi(self)
self.listWidget = QListWidget()
self.grid = QGridLayout(self)
self.grid.addWidget(self.invalidLogin_label, 0, 0, 1, 2)
self.grid.addWidget(self.listWidget, 1, 0, 1, 2)
self.grid.addWidget(self.yes_button, 2, 0)
self.grid.addWidget(self.no_button, 2, 1)
class Ui_ViewAllRooms(object):
def setupUi(self, ViewAllRooms):
ViewAllRooms.setObjectName("ViewAllRooms")
ViewAllRooms.resize(671, 347)
ViewAllRooms.setMinimumSize(QtCore.QSize(671, 347))
ViewAllRooms.setMaximumSize(QtCore.QSize(671, 347))
self.centralwidget = QtWidgets.QWidget(ViewAllRooms)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 100, 651, 181))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(6)
self.tableWidget.setRowCount(5)
self.tableWidget.setHorizontalHeaderLabels("P1 P2 P3 P4 P5 P6".split())
self.tableWidget.setVerticalHeaderLabels("C101 C214 C320 F04 E201".split())
for i in range(self.tableWidget.rowCount()):
for j in range(self.tableWidget.columnCount()):
item = QtWidgets.QTableWidgetItem()
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.tableWidget.setItem(i, j, item)
self._checked_items = []
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 290, 231, 41))
self.label.setObjectName("label")
self.book_Button = QtWidgets.QPushButton(self.centralwidget)
self.book_Button.setGeometry(QtCore.QRect(300, 290, 75, 23))
self.book_Button.setObjectName("book_Button")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(590, 10, 75, 23))
self.backButton.setObjectName("backButton")
self.login_title = QtWidgets.QLabel(self.centralwidget)
self.login_title.setGeometry(QtCore.QRect(220, 10, 231, 38))
font = QtGui.QFont()
font.setFamily("Gloucester MT Extra Condensed")
font.setPointSize(25)
font.setBold(False)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(50)
self.login_title.setFont(font)
self.login_title.setLayoutDirection(QtCore.Qt.LeftToRight)
self.login_title.setAutoFillBackground(False)
self.login_title.setAlignment(QtCore.Qt.AlignCenter)
self.login_title.setObjectName("login_title")
self.date_label = QtWidgets.QLabel(self.centralwidget)
self.date_label.setGeometry(QtCore.QRect(240, 70, 191, 21))
self.date_label.setAlignment(QtCore.Qt.AlignCenter)
self.date_label.setObjectName("date_label")
self.nextDayButton = QtWidgets.QPushButton(self.centralwidget)
self.nextDayButton.setGeometry(QtCore.QRect(590, 70, 75, 23))
self.nextDayButton.setObjectName("nextDayButton")
ViewAllRooms.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(ViewAllRooms)
self.statusbar.setObjectName("statusbar")
ViewAllRooms.setStatusBar(self.statusbar)
self.retranslateUi(ViewAllRooms)
QtCore.QMetaObject.connectSlotsByName(ViewAllRooms)
def retranslateUi(self, ViewAllRooms):
_translate = QtCore.QCoreApplication.translate
ViewAllRooms.setWindowTitle(_translate("ViewAllRooms", "MainWindow"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "C101"))
item.setToolTip("Hall")
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "C214"))
item.setToolTip("Study Space")
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "C320"))
item.setToolTip("Computer")
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "F04"))
item.setToolTip("Mac Room")
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "E201"))
item.setToolTip("Computer")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "P1"))
item.setToolTip("8:55")
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "P2"))
item.setToolTip("9:55")
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "P3"))
item.setToolTip("11:15")
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "P4"))
item.setToolTip("12:15")
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "P5"))
item.setToolTip("2:00")
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("ViewAllRooms", "P6"))
item.setToolTip("3:15")
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.book_Button.setText(_translate("ViewAllRooms", "Book"))
self.backButton.setText(_translate("ViewAllRooms", "Back"))
self.login_title.setText(_translate("ViewAllRooms", "VIEW ALL ROOMS"))
self.date_label.setText(_translate("ViewAllRooms", "Date:14 January 2019"))
self.label.setText(_translate("ViewAllRooms", "Hover over a room number or period for\n"
"extra information"))
self.nextDayButton.setText(_translate("ViewAllRooms", "Next Day"))
class ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
def __init__(self, parent=None):
super(ViewAllRooms, self).__init__(parent)
self.setupUi(self)
self.dialogBook = DialogBook() #Ui_DialogBook() #
# self.book_Button.clicked.connect(self.dialogBook.show)
self.book_Button.clicked.connect(self.book_clicked)
#QtCore.pyqtSlot()
def book_clicked(self):
self._checked_items = []
# items = []
for i in range(self.tableWidget.rowCount()):
for j in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(i, j)
if item.checkState() == QtCore.Qt.Checked:
# items.append(item)
# r = it.row()
# c = it.column()
v = self.tableWidget.horizontalHeaderItem(j).text()
h = self.tableWidget.verticalHeaderItem(i).text()
self._checked_items.append([h, v, self.tableWidget.item(i, j).text()])
self.dialogBook.listWidget.clear()
self.dialogBook.listWidget.addItems([ "{} {} {}".format(h, v, it) for h, v, it in self._checked_items])
self.dialogBook.show()
"""
print("items->", items)
for it in items:
r = it.row()
c = it.column()
v, h = self.tableWidget.horizontalHeaderItem(c).text(), self.tableWidget.verticalHeaderItem(r).text()
print("item---", self.tableWidget.item(r, c).text())
self._checked_items.append([h, v])
aylm = (self.checked_items())
aylm_len = len(aylm)
for nm in range (0, aylm_len):
print (aylm[nm])
def checked_items(self):
return self._checked_items
"""
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DialogBook = ViewAllRooms()
# DialogBook = QtWidgets.QDialog()
# ui = DialogBook()
# ui.setupUi(DialogBook)
DialogBook.show()
sys.exit(app.exec_())

Pyqt Display data from mysql in tablewidget incomplete

I'm newbie in PyQt,recently I want to use pyqt to do a data management system for athletes data.I create 50*3 blank Item.But when I delete data and then insert data, the data is not completely displayed on the interface.And when I add data,there is a mistake
a=self.tableWidget.horizontalHeaderItem(col).text()
AttributeError: 'NoneType' object has no attribute 'text'.
I use Qt designer to write UI program
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(841, 690)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(90, 60, 661, 401))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.lineEdit = QtGui.QLineEdit(self.tab)
self.lineEdit.setGeometry(QtCore.QRect(80, 240, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.lineEdit_2 = QtGui.QLineEdit(self.tab)
self.lineEdit_2.setGeometry(QtCore.QRect(80, 280, 113, 20))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.lineEdit_3 = QtGui.QLineEdit(self.tab)
self.lineEdit_3.setGeometry(QtCore.QRect(80, 320, 113, 20))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.label = QtGui.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(10, 240, 54, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(10, 280, 51, 20))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.tab)
self.label_3.setGeometry(QtCore.QRect(10, 320, 54, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.pushButton = QtGui.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(550, 240, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(550, 280, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.tab)
self.pushButton_3.setGeometry(QtCore.QRect(550, 320, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.tableWidget = QtGui.QTableWidget(self.tab)
self.tableWidget.setGeometry(QtCore.QRect(20, 10, 611, 192))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(50)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
MainWindow.setCentralWidget(self.centralwidget)
self.QYolk = QtGui.QMenuBar(MainWindow)
self.QYolk.setGeometry(QtCore.QRect(0, 0, 841, 23))
self.QYolk.setObjectName(_fromUtf8("QYolk"))
MainWindow.setMenuBar(self.QYolk)
self.label.setBuddy(self.lineEdit)
self.label_2.setBuddy(self.lineEdit_2)
self.label_3.setBuddy(self.lineEdit_3)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label.setText(_translate("MainWindow", " name0", None))
self.label_2.setText(_translate("MainWindow", "name1", None))
self.label_3.setText(_translate("MainWindow", "name2", None))
self.pushButton.setText(_translate("MainWindow", "add", None))
self.pushButton_2.setText(_translate("MainWindow", "save", None))
self.pushButton_3.setText(_translate("MainWindow", "delete", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2", None))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "name0", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "name1", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "name2", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "player1", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "player2", None))
Here is my back-end program.I use cellchange to get the changed data.Is it a good idea?
from PyQt4 import QtCore, QtGui,QtSql
import sys
class StartQt4(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
self.db.setHostName("localhost")
self.db.setDatabaseName("test")
self.db.setUserName("root")
self.db.setPassword("")
self.q=QtSql.QSqlQuery()
if (self.db.open()):
self.showdata()
else:
print "failed"
QtCore.QObject.connect(self.tableWidget, QtCore.SIGNAL("cellChanged(int, int)"), self.changedata)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.adddata)
QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL("clicked()"), self.deldata)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.savedata)
def adddata(self):
name0 = str(self.lineEdit.text())
name1 = str(self.lineEdit_2.text())
name2 = str(self.lineEdit_3.text())
self.q.exec_("insert into player values ('%s','%s','%s')" %(name0,name1,name2))
#lastrow=self.tableWidget.rowCount()
#self.tableWidget.insertRow(lastrow)
self.showdata()
def changedata(self,row,col):
co=self.tableWidget.columnCount()
row =self.tableWidget.currentRow()
a= self.tableWidget.selectedItems()
col=self.tableWidget.currentColumn()
a=self.tableWidget.horizontalHeaderItem(col).text()
if (col < (co-1)):
b=self.tableWidget.horizontalHeaderItem(col+1).text()
t=self.tableWidget.item(row, col+1).text()
elif (col == (co-1)):
b=self.tableWidget.horizontalHeaderItem(col-1).text()
t=self.tableWidget.item(row, col-1).text()
dt=self.tableWidget.currentItem().text()
self.q.exec_("update qiu set %s = '%s' where %s = %s " %(a,dt,b,t))
def savedata(self):
self.q.exec_("insert into player1 select distinct * from player")
def deldata(self):
#self.tableWidget.clear()
row=self.tableWidget.currentRow()
print row
a=self.tableWidget.horizontalHeaderItem(0).text()
x=self.tableWidget.item(row,0).text()
self.q.exec_("delete from qiu where %s=%s" %(a,x))
self.showdata()
def showdata(self):
self.q.exec_("select * from qiu")
col=self.tableWidget.columnCount()
for i in range(0,self.tableWidget.rowCount()):
self.q.next()
for j in range(col):
x=self.q.value(j).toString()
self.tableWidget.setItem(i, j, QtGui.QTableWidgetItem(x))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())
Appreciate very much

Categories

Resources