Stack Overflow на русском Asked by imapw_ on January 21, 2021
Я хотел сделать так, чтобы при нажатии item 0
в listwidget
фон двух окон менялся на красный. Если же потом кликнуть на item 0
фон обратно должен стать белым и после этого еще нажать, то он красным не станет.
Почему не меняется фон второго окна? Я хочу сделать так, что при нажатии Red background
в listwidget
фон обоих окон менялся, но сейчас он почему-то не меняется, также после клика уже на White background
фон менялся на белый обратно на обеих окнах и если еще раз нажать, то он менялся на красный!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(251, 192)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 140, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(0, 0, 251, 191))
self.label_1.setText("")
self.label_1.setObjectName("label_1")
MainWindow.setCentralWidget(self.centralwidget)
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", "Back"))
class ExampleApp2(QtWidgets.QMainWindow, Ui_MainWindow2):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.myclose)
def myclose(self):
self.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(251, 191)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(0, 0, 256, 191))
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(21)
item.setFont(font)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 170, 75, 23))
self.pushButton.setObjectName("pushButton")
self.listWidget.raise_()
self.pushButton.raise_()
MainWindow.setCentralWidget(self.centralwidget)
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", "Open Window"))
class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.listWidget.insertItem(0, "Red background")
self.listWidget.setFrameShape(QtWidgets.QFrame.NoFrame)
self.listWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.listWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.listWidget.setFont (QFont ("Helvetica", 20));
self.listWidget.setStyleSheet("QListWidget::item:hover {background: rgba(158, 158, 158, 40);}n"
"QListWidget::item:selected {background: transparent;}n"
"QListWidget::item:selected {color: black;}n"
"")
self.listWidget.itemClicked.connect(self.openWindow)
self.pushButton.clicked.connect(self.click)
def click(self):
self.exampleApp_2 = ExampleApp2(self)
self.exampleApp_2.show()
def openWindow(self, item):
if item.text() == "Red background":
self.exampleApp_2 = ExampleApp2(self)
self.listWidget.removeItemWidget(item)
self.listWidget.takeItem(0)
self.listWidget.setCurrentRow(0)
self.listWidget.insertItem(0, "White background")
self.listWidget.setStyleSheet("background-color: red;")
self.exampleApp_2.label_1.setStyleSheet("background-color: red;")
else:
self.listWidget.setStyleSheet("background-color: white;")
self.exampleApp_2.label_1.setStyleSheet("background-color: white;")
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
window = ExampleApp()
window.show()
sys.exit(app.exec_())
В чем же ошибка?
Я отметит для вас строки, которые отредактировал.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setGeometry(QtCore.QRect(850, 270, 251, 192)) #resize(251, 192)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self)
# self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 140, 75, 23))
self.pushButton.setObjectName("pushButton_1")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(0, 0, 251, 191))
self.label_1.setText("12312231223")
self.label_1.setObjectName("label_1")
MainWindow.setCentralWidget(self.centralwidget)
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", "Back"))
class ExampleApp2(QtWidgets.QMainWindow, Ui_MainWindow2):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.myclose)
def myclose(self):
self.hide() #close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(251, 191)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(0, 0, 256, 191))
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(21)
item.setFont(font)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 170, 75, 23))
self.pushButton.setObjectName("pushButton")
self.listWidget.raise_()
self.pushButton.raise_()
MainWindow.setCentralWidget(self.centralwidget)
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", "Open Window"))
class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.listWidget.insertItem(0, "Red background")
self.listWidget.setFrameShape(QtWidgets.QFrame.NoFrame)
self.listWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.listWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.listWidget.setFont (QFont ("Helvetica", 20));
self.listWidget.setStyleSheet("QListWidget::item:hover {background: rgba(158, 158, 158, 40);}n"
"QListWidget::item:selected {background: transparent;}n"
"QListWidget::item:selected {color: black;}n"
"")
self.listWidget.itemClicked.connect(self.openWindow)
self.pushButton.clicked.connect(self.click)
self.exampleApp_2 = ExampleApp2(self) # +++
def click(self):
# self.exampleApp_2 = ExampleApp2(self)
self.exampleApp_2.show()
def openWindow(self, item):
if item.text() == "Red background":
# self.exampleApp_2 = ExampleApp2(self)
self.listWidget.removeItemWidget(item)
self.listWidget.takeItem(0)
self.listWidget.setCurrentRow(0)
self.listWidget.insertItem(0, "White background")
self.listWidget.setStyleSheet("background-color: red;")
self.exampleApp_2.label_1.setStyleSheet("background-color: red;")
else:
self.listWidget.setStyleSheet("background-color: white;")
self.exampleApp_2.label_1.setStyleSheet("background-color: white;")
item.setText("Red background") # +++
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
window = ExampleApp()
window.show()
sys.exit(app.exec_())
Correct answer by S. Nick on January 21, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP