Currently, I have some code that creates a QTableWidget. When you click on a QTableWidget cell, a label appears which shows the name of the item you clicked. Then, there is a menu button at the top which, when clicked, takes you back to the QTableWidget. I do this using QStackedWidgets, and everything works well the first time. However, the second time I click an item, the label doesn't change its text. For example, if the first time, I clicked a QTableWidget cell called "NUM 1"
, the label would appear with the text NUM 1
. Then, I click the menu button. However, the second time when I click a different cell, such as "NUM 2"
, the label doesn't change and still shows NUM 1
.
How could I fix this. I have tried using label.update()
, but it doesn't work.
Here is my code:
from PyQt5 import QtWidgets, QtCore, QtGui
import sys
class MyWindow(QtWidgets.QMainWindow):
#Define The Init
def __init__(self):
super(MyWindow, self).__init__()
self.ID = ""
self.initUI()
self.setGeometry(200, 200, 1325, 955)
self.menuButton = QtWidgets.QLabel(self)
self.menuButton.setText("Menu")
self.menuButton.mousePressEvent = self.GoToMenu
Central = QtWidgets.QWidget(self)
self.setCentralWidget(Central)
self.stack1 = QtWidgets.QWidget()
self.stack2 = QtWidgets.QWidget()
self.MainMenu()
self.Stack = QtWidgets.QStackedWidget(self)
self.Stack.addWidget(self.stack1)
self.Stack.addWidget(self.stack2)
vboxMain = QtWidgets.QVBoxLayout()
hbox = QtWidgets.QHBoxLayout(self)
hbox.addWidget(self.menuButton)
hbox.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignCenter)
vboxMain.addLayout(hbox)
vboxMain.addWidget(self.Stack)
Central.setLayout(vboxMain)
self.Stack.setCurrentIndex(0)
def initUI(self):
pass
def MainMenu(self):
self.Table = QtWidgets.QTableWidget()
self.Table.setMouseTracking(True)
self.rowName = ["NUM 1", "NUM 2", "NUM 3", "NUM 4"]
self.Table.setColumnCount(2)
self.Table.setRowCount(len(self.rowName))
self.verticalHeader = self.Table.verticalHeader()
self.Table.verticalHeader().setVisible(False)
self.Table.setFocusPolicy(QtCore.Qt.NoFocus)
self.horizontalHeader = self.Table.horizontalHeader()
self.horizontalHeader.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.horizontalHeader.setDefaultAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.Table.setHorizontalHeaderLabels(("Name;Point").split(";"))
for section in range(self.verticalHeader.count()):
self.Table.setItem(section, 0, QtWidgets.QTableWidgetItem(self.rowName[section]))
self.Table.cellClicked.connect(self.cell_was_clicked)
hboxList = QtWidgets.QHBoxLayout()
hboxList.addStretch()
hboxList.addWidget(self.Table)
hboxList.addStretch()
vboxMainMenu = QtWidgets.QVBoxLayout()
vboxMainMenu.addLayout(hboxList)
vboxMainMenu.addStretch()
self.stack1.setLayout(vboxMainMenu)
def GoToMenu(self, eve):
self.Stack.setCurrentIndex(0)
def cell_was_clicked(self, row, column):
self.ViewLabel(self.Table.item(row, 0).text())
self.Stack.setCurrentIndex(1)
def ViewLabel(self, IDL):
self.lbl = QtWidgets.QLabel()
self.lbl.setText(IDL) ###...THE PROBLEM IS HERE...###
hboxShow = QtWidgets.QHBoxLayout()
hboxShow.addStretch()
hboxShow.addWidget(self.lbl)
hboxShow.addStretch()
vboxShow = QtWidgets.QVBoxLayout()
vboxShow.addLayout(hboxShow)
self.stack2.setLayout(vboxShow)
app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
question from:
https://stackoverflow.com/questions/65866146/label-wont-update 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…