有没有办法从一个窗口更改一个窗口的标签?

时间:2019-04-11 12:11:56

标签: python pyqt5

我有一些代码可以检查用户点击了哪个复选框。然后将它们放入数组(aylm)。用户单击预订按钮后,会弹出一个小确认窗口,表明您确定要预订这些房间。在此确认窗口中,我希望标签说您确定要预订(aylm)?这样用户可以看到所有选定的房间和时间。我不确定如何执行此操作,因为我使用的是一个将所有较小的窗口连接在一起的主窗口,您可以在下面的代码中看到。


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

PyQt设计器为Ui_DialogBook自动生成的代码如下:

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

1 个答案:

答案 0 :(得分:1)

尝试一下:

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_())

enter image description here