Related
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
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_())
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_())
I want to pass self.user = QtGui.QLineEdit(MainWindow) variable from Login.py to officer = str(self.tocomb.currentText()) in ingooi.py
My 3 GUI windows
My program consists of various files where I import the classes from other files as needed.
The 3 main files consists of :
Login.py ( My Login window where user logs in )
page22.py ( User selects to enter/view data )
ingooi.py ( User enters data then adds the data to access database via Submit
Button
Please see the code for these 3 main files below.
Login.py
from PyQt4 import QtCore, QtGui
from ingooi import Ui_NextPage
from page22 import Ui_Page1
import pypyodbc
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_Logged(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(389, 235)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Login_button = QtGui.QPushButton(self.centralwidget)
self.Login_button.setGeometry(QtCore.QRect(90, 150, 75, 23))
self.Login_button.setObjectName(_fromUtf8("Login_button"))
self.user = QtGui.QLineEdit(MainWindow)
self.user.setGeometry(QtCore.QRect(100, 30, 113, 20))
self.user.setObjectName(_fromUtf8("user"))
self.password = QtGui.QLineEdit(MainWindow)
self.password.setGeometry(QtCore.QRect(100, 70, 113, 20))
self.password.setObjectName(_fromUtf8("password"))
self.password.setEchoMode(QtGui.QLineEdit.Password)
self.user_label = QtGui.QLabel(self.centralwidget)
self.user_label.setGeometry(QtCore.QRect(30, 30, 46, 13))
self.user_label.setObjectName(_fromUtf8("user_label"))
self.Password_label = QtGui.QLabel(self.centralwidget)
self.Password_label.setGeometry(QtCore.QRect(30, 70, 46, 13))
self.Password_label.setObjectName(_fromUtf8("Password_label"))
self.status_label = QtGui.QLabel(self.centralwidget)
self.status_label.setGeometry(QtCore.QRect(100, 110, 121, 16))
self.status_label.setText(_fromUtf8(""))
self.status_label.setObjectName(_fromUtf8("status_label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 389, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.Login_button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.slot1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Login ", None))
self.Login_button.setText(_translate("MainWindow", "Login ", None))
self.user_label.setText(_translate("MainWindow", "User", None))
self.Password_label.setText(_translate("MainWindow", "Password", None))
def WW(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Page1()
self.ui.setupUi(self.window)
self.window.show()
MainWindow.hide()
def slot1(self):
uName = self.user.text()
uPass = self.password.text()
print uName
print uPass
conn = pypyodbc.connect('DRIVER={Driver do Microsoft Access (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5
;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=//ICASACTNFS02/Share/New;DBQ=//ICASACTNFS02/Share/ New/snake.mdb')
cursor = conn.cursor()
sql = "SELECT * FROM gtable WHERE User = '%s' AND pwd = '%s'"%
(uName,uPass)
see = cursor.execute(sql)
if (len (see.fetchall()) > 0) :
print " User OK !"
self.WW()
conn.close()
else:
self.status_label.setText(_fromUtf8("Try ma wee"))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_Logged()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Page22.py
from PyQt4 import QtCore, QtGui
from ingooi import Ui_NextPage
from tafel import Ui_Main
from tafel2 import Ui_TaPage
import pypyodbc
import sys
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_Page1(object):
def setupUi(self, Page1):
Page1.setObjectName(_fromUtf8("Page1"))
Page1.resize(586, 617)
self.centralwidget = QtGui.QWidget(Page1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(130, 40, 300, 61))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.dataenter)
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(130, 130, 291, 61))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_2.clicked.connect(self.dataview)
self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(130, 220, 291, 61))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(self.Taview)
self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(130, 310, 291, 61))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton_4.clicked.connect(self.exit)
Page1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(Page1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 586, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
Page1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(Page1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
Page1.setStatusBar(self.statusbar)
self.retranslateUi(Page1)
QtCore.QMetaObject.connectSlotsByName(Page1)
def retranslateUi(self, Page1):
Page1.setWindowTitle(_translate("Page1", "MainWindow", None))
self.pushButton.setText(_translate("Page1", "Data Entry ", None))
self.pushButton_2.setText(_translate("Page1", "View Data ", None))
self.pushButton_3.setText(_translate("Page1", " Type Approved Device
List ", None))
self.pushButton_4.setText(_translate("Page1", "EXIT ", None))
def dataenter(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_NextPage()
self.ui.setupUi(self.window)
self.window.show()
def dataview(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Main()
self.ui.setupUi(self.window)
self.window.show()
def Taview(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_TaPage()
self.ui.setupUi(self.window)
self.window.show()
def exit(self):
sys.exit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Page1 = QtGui.QMainWindow()
ui = Ui_Page1()
ui.setupUi(Page1)
Page1.show()
sys.exit(app.exec_())
ingooi.py
from PyQt4 import QtCore, QtGui
import pypyodbc
from tafel import Ui_Main
import subprocess
import sys
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_NextPage(object):
def ViewPage(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Main()
self.ui.setupUi(self.window)
self.window.show()
def load(self):
mydate = self.insp_date_comb.date()
realdate = mydate.toString("dd/MM/yyyy")
lic_name = str(self.lic_name_comb.currentText())
lic_num = str(self.lic_nr_comb.currentText())
systm = str(self.sys_name_comb.currentText())
Loc = str(self.Loc_comb.currentText())
txfreq = str(self.tx_comb.currentText())
rxfreq = str(self.rx_comb.currentText())
fwdpwr = str(self.fwdpwr_comb.currentText())
rflpwr = str(self.rflpwr_comb.currentText())
officer = str(self.tocomb.currentText())
print lic_name
print realdate
conn = pypyodbc.connect('DRIVER={Driver do Microsoft Access (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5
;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=//ICASACTNFS02/Share/New;DBQ=//ICASACTNFS02/Share /New/snake.mdb')
cursor = conn.cursor()
# query = "DELETE FROM measdata WHERE RFL_PWR = '%s'" %(0)
query = "INSERT INTO measdata (Licensee,Lic_number,System,Location,Insp_date,TX,RX,FWD_PWR,RFL_PWR,TO) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (lic_name,lic_num,systm,Loc,realdate,txfreq,rxfreq,fwdpwr,rflpwr,officer)
cursor.execute(query)
conn.commit()
conn.close()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(544, 516)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.lic_name_comb = QtGui.QComboBox(self.centralwidget)
self.lic_name_comb.setGeometry(QtCore.QRect(120, 20, 181, 22))
self.lic_name_comb.setEditable(True)
self.lic_name_comb.setObjectName(_fromUtf8("lic_name_comb"))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.setItemText(0, _fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 20, 81, 31))
self.label.setObjectName(_fromUtf8("label"))
self.tolabel = QtGui.QLabel(self.centralwidget)
self.tolabel.setGeometry(QtCore.QRect(30, 350, 70, 23))
self.tolabel.setObjectName(_fromUtf8("tolabel"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 50, 71, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lic_nr_comb = QtGui.QComboBox(self.centralwidget)
self.lic_nr_comb.setGeometry(QtCore.QRect(120, 50, 101, 22))
self.lic_nr_comb.setEditable(True)
self.lic_nr_comb.setObjectName(_fromUtf8("lic_nr_comb"))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.setItemText(0, _fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.label_8 = QtGui.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(30, 80, 71, 21))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.sys_name_comb = QtGui.QComboBox(self.centralwidget)
self.sys_name_comb.setGeometry(QtCore.QRect(120, 80, 101, 22))
self.sys_name_comb.setEditable(True)
self.sys_name_comb.setObjectName(_fromUtf8("sys_name_comb"))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.setItemText(0, _fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.insp_date_comb = QtGui.QDateEdit(self.centralwidget)
self.insp_date_comb.setGeometry(QtCore.QRect(120, 150, 110, 22))
self.insp_date_comb.setObjectName(_fromUtf8("insp_date_comb"))
self.insp_date_comb.setDateTime(QtCore.QDateTime.currentDateTime())
self.insp_date_comb.setCalendarPopup(True)
self.label_9 = QtGui.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(30, 150, 81, 16))
self.label_9.setObjectName(_fromUtf8("label_9"))
self.submit_btn = QtGui.QPushButton(self.centralwidget)
self.submit_btn.setGeometry(QtCore.QRect(20, 415, 70, 23))
self.submit_btn.setObjectName(_fromUtf8("submit_btn"))
self.submit_btn.clicked.connect(self.load)
self.Loc_comb = QtGui.QComboBox(self.centralwidget)
self.Loc_comb.setGeometry(QtCore.QRect(120, 110, 101, 22))
self.Loc_comb.setEditable(True)
self.Loc_comb.setObjectName(_fromUtf8("Loc_comb"))
self.Loc_comb.addItem(_fromUtf8(""))
self.Loc_comb.setItemText(0, _fromUtf8(""))
self.Loc_comb.addItem(_fromUtf8(""))
self.Loc_comb.addItem(_fromUtf8(""))
self.label_10 = QtGui.QLabel(self.centralwidget)
self.label_10.setGeometry(QtCore.QRect(30, 110, 71, 21))
self.label_10.setObjectName(_fromUtf8("label_10"))
self.rx_comb = QtGui.QComboBox(self.centralwidget)
self.rx_comb.setGeometry(QtCore.QRect(120, 220, 69, 22))
self.rx_comb.setEditable(True)
self.rx_comb.setObjectName(_fromUtf8("rx_comb"))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.setItemText(0, _fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.tocomb = QtGui.QComboBox(self.centralwidget)
self.tocomb.setGeometry(QtCore.QRect(120, 348, 110, 22))
self.tocomb.setEditable(True)
self.tocomb.setObjectName(_fromUtf8("rx_comb"))
self.tocomb.addItem(_fromUtf8(""))
self.tocomb.setItemText(0, _fromUtf8(""))
self.tocomb.addItem(_fromUtf8("VAN NIEKERK B.A"))
self.tocomb.addItem(_fromUtf8("ERASMUS C.M"))
self.tocomb.addItem(_fromUtf8("FRANSMAN G.F"))
self.tocomb.addItem(_fromUtf8("COOK H.A"))
self.tocomb.addItem(_fromUtf8("MCQUIRE L."))
self.tocomb.addItem(_fromUtf8("Gertse RS"))
self.label_6 = QtGui.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(30, 220, 71, 21))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.fwdpwr_comb = QtGui.QComboBox(self.centralwidget)
self.fwdpwr_comb.setGeometry(QtCore.QRect(120, 280, 69, 22))
self.fwdpwr_comb.setEditable(True)
self.fwdpwr_comb.setObjectName(_fromUtf8("fwdpwr_comb"))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.setItemText(0, _fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb = QtGui.QComboBox(self.centralwidget)
self.rflpwr_comb.setGeometry(QtCore.QRect(120, 310, 69, 22))
self.rflpwr_comb.setEditable(True)
self.rflpwr_comb.setObjectName(_fromUtf8("rflpwr_comb"))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.setItemText(0, _fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 310, 71, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(30, 190, 71, 21))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 280, 71, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.tx_comb = QtGui.QComboBox(self.centralwidget)
self.tx_comb.setGeometry(QtCore.QRect(120, 190, 69, 22))
self.tx_comb.setEditable(True)
self.tx_comb.setObjectName(_fromUtf8("tx_comb"))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.setItemText(0, _fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 450, 90, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 544, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lic_name_comb.setItemText(1, _translate("MainWindow", "Comms 1 ", None))
self.lic_name_comb.setItemText(2, _translate("MainWindow", "Comms 2", None))
self.lic_name_comb.setItemText(3, _translate("MainWindow", "Comms 3", None))
self.label.setText(_translate("MainWindow", "Licensee", None))
self.label_2.setText(_translate("MainWindow", "License nr", None))
self.lic_nr_comb.setItemText(1, _translate("MainWindow", "12345689", None))
self.lic_nr_comb.setItemText(2, _translate("MainWindow", "13634568", None))
self.lic_nr_comb.setItemText(3, _translate("MainWindow", "56345689", None))
self.label_8.setText(_translate("MainWindow", "System", None))
self.sys_name_comb.setItemText(1, _translate("MainWindow", "Repeater ", None))
self.sys_name_comb.setItemText(2, _translate("MainWindow", "Telemetry", None))
self.sys_name_comb.setItemText(3, _translate("MainWindow", "Trunking ", None))
self.sys_name_comb.setItemText(4, _translate("MainWindow", "Alarm", None))
self.sys_name_comb.setItemText(5, _translate("MainWindow", "Relay", None))
self.insp_date_comb.setDisplayFormat(_translate("MainWindow", "d/M/yyyy", None))
self.label_9.setText(_translate("MainWindow", "Inspection Date", None))
self.submit_btn.setText(_translate("MainWindow", "Submit", None))
self.Loc_comb.setItemText(1, _translate("MainWindow", "Location 1 ", None))
self.Loc_comb.setItemText(2, _translate("MainWindow", "Location 2 ", None))
self.label_10.setText(_translate("MainWindow", "Location", None))
self.tolabel.setText(_translate("MainWindow", "OFFICER", None))
self.rx_comb.setItemText(1, _translate("MainWindow", "100", None))
self.rx_comb.setItemText(2, _translate("MainWindow", "200", None))
self.rx_comb.setItemText(3, _translate("MainWindow", "300", None))
self.rx_comb.setItemText(4, _translate("MainWindow", "400", None))
self.rx_comb.setItemText(5, _translate("MainWindow", "500", None))
self.label_6.setText(_translate("MainWindow", "RX FREQ", None))
self.fwdpwr_comb.setItemText(1, _translate("MainWindow", "10", None))
self.fwdpwr_comb.setItemText(2, _translate("MainWindow", "20", None))
self.fwdpwr_comb.setItemText(3, _translate("MainWindow", "30", None))
self.rflpwr_comb.setItemText(1, _translate("MainWindow", "1", None))
self.rflpwr_comb.setItemText(2, _translate("MainWindow", "2", None))
self.rflpwr_comb.setItemText(3, _translate("MainWindow", "3", None))
self.rflpwr_comb.setItemText(4, _translate("MainWindow", "4", None))
self.rflpwr_comb.setItemText(5, _translate("MainWindow", "5", None))
self.label_4.setText(_translate("MainWindow", "REFL PWR", None))
self.label_5.setText(_translate("MainWindow", "TX FREQ", None))
self.label_3.setText(_translate("MainWindow", "FWD PWR ", None))
self.tx_comb.setItemText(1, _translate("MainWindow", "100", None))
self.tx_comb.setItemText(2, _translate("MainWindow", "200", None))
self.tx_comb.setItemText(3, _translate("MainWindow", "300", None))
self.tx_comb.setItemText(4, _translate("MainWindow", "400", None))
self.tx_comb.setItemText(5, _translate("MainWindow", "500", None))
self.pushButton.setText(_translate("MainWindow", "View Data ", None))
self.pushButton.clicked.connect(self.ViewPage)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_NextPage()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem is in your design, you should not think that each file is a different program, it is really only one.
Another recommendation is that you do not modify the code generated by QtDesigner, this is not necessary also if you want to modify something in the GUI will erase all the above, for example if you have a design for example the Login.py:
class Ui_Logged(object):
def setupUi(self, MainWindow):
[...]
Then you must create another class that implements the logic
class Login(QtGui.QMainWindow, Login.Ui_Logged):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
[...]
then in your case you have to Page1() member of the class and show it when you call WW(), you should not create it just open it
class Login(QtGui.QMainWindow, Login.Ui_Logged):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Login_button.clicked(self.slot1)
self.window = Page1()
def WW(self):
self.window.show()
self.hide()
The same thing you have to do in Page1() with each slot. NextPage() is the class that implements the logic of Ui_NextPage()
class Page1(QtGui.QMainWindow, Page22.Ui_Page1)
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.pushButton.clicked.connect(self.dataenter)
self.pushButton_2.clicked.connect(self.dataview)
self.pushButton_3.clicked.connect(self.Taview)
self.pushButton_4.clicked.connect(self.exit)
self.nextPage = NextPage()
def dataenter(self):
self.nextPage.show()
[...]
So if we have that logic it's easy to call to access the QComboBox from slot():
def slot1(self):
uName = self.user.text()
uPass = self.password.text()
combo = self.window.nextPage.tocomb
items = [combo.itemText(i) for i in range(combo.count())]
if uName not in items:
combo.addItem(uName)
combo.setCurrentTex(uName)
I have tried to order your code, maybe have import problems but I think they should be minimal so you can fix them, run the file main.py. The complete code can be found at the following link.
I am new to PYQT and I am trying to get a feel for it. I am trying to populate a table widget with 5 different push buttons. I need to clear out the table between each button push, but would like to leave the headers I have. I have tried a tablewidget clearcontents. but that clears everything and I am unsure of where to place it properly so it will clear the contents before each button push, that brings in the new info.
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_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(761, 637)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.get_output1_statement)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(0, 580, 201, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton.clicked.connect(self.get_output2_statement)
self.pushButton_3 = QtGui.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(0, 610, 201, 31))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(self.get_output3_statement)
self.pushButton_4 = QtGui.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(250, 580, 201, 31))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton.clicked.connect(self.get_output4_statement)
self.pushButton_5 = QtGui.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(510, 580, 201, 31))
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.pushButton.clicked.connect(self.get_output5_statement)
self.tableWidget = QtGui.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
self.pushButton_2.setText(_translate("Form", "button2", None))
self.pushButton_3.setText(_translate("Form", "button3", None))
self.pushButton_4.setText(_translate("Form", "button4", None))
self.pushButton_5.setText(_translate("Form", "button5", None))
You can use the clearContents() method when you set the table headers in an appropriate way.
change:
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
to:
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
self.tableWidget.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setHorizontalHeaderItem(3, QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setHorizontalHeaderItem(4, QtGui.QTableWidgetItem("column_4"))
EDIT Extra code after comments
Maybe this helps you? I tried keeping as much of your code.
class Ui_Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
self.setObjectName(_fromUtf8("Form"))
self.resize(761, 637)
self.pushButton = QtGui.QPushButton()
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.connect(self.pushButton, QtCore.SIGNAL("released()"), self.get_output1_statement)
self.tableWidget = QtGui.QTableWidget()
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
layout = QtGui.QVBoxLayout()
layout.addWidget(self.tableWidget)
layout.addWidget(self.pushButton)
self.setLayout(layout)
for i in range(1, 5):
self.tableWidget.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem("column_{0}".format(i)))
self.retranslateUi()
def retranslateUi(self):
self.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
def get_output1_statement(self):
self.tableWidget.clearContents()
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("add some data"))
app = QtGui.QApplication(sys.argv)
form = Ui_Form()
form.show()
app.exec_()