[英]Pyqt5 updated Widget is not added to the layout though the widget is updated
I am trying to show set of actions when a button is clicked and other set of options if another button is clicked. 我试图显示单击按钮时的一组动作,如果单击另一个按钮,则显示其他选项。 Below is the code:
下面是代码:
class Screen(QWidget):
def __init__(self):
super(Screen, self).__init__()
layout = QHBoxLayout(self)
self.all_running()
layout.addWidget(self.running_full_widget)
self.actions('1')
layout.addWidget(self.actions_full_widget)
self.setLayout(layout)
self.show()
def all_running(self):
self.running_full_widget = QWidget()
runnning_full_layout= QVBoxLayout()
button1 = QPushButton("btn1")
button2 = QPushButton("btn2")
button1.clicked.connect(lambda: self.actions('2'))
button2.clicked.connect(lambda: self.actions('3'))
runnning_full_layout.addWidget(button1)
runnning_full_layout.addWidget(button2)
self.running_full_widget.setLayout(runnning_full_layout)
def actions(self,value):
self.actions_full_widget= QWidget()
val = int(value)
print(val)
actions_layout = QVBoxLayout()
for i in range(val):
actions_item = QLabel(str(i))
actions_layout.addWidget(actions_item)
self.actions_full_widget.setLayout(actions_layout)
app = QApplication(sys.argv)
Gui = Screen()
sys.exit(app.exec_())
When button is clicked i can see the value is updated but it is not updated in the main layout. 单击按钮后,我可以看到该值已更新,但在主布局中未更新。 how can i dynamically update the widgets ?
如何动态更新小部件? How can I do that in cases where i need to add widgets based on a dynamic value.
如果我需要基于动态值添加小部件,该怎么办。 did i miss anything with signals and slots?
我错过了信号和插槽吗? Kindly correct me if i am wrong.
如果我错了,请纠正我。 Thanks
谢谢
Your code was almost there. 您的代码几乎在那里。 The problem you are seeing is the widgets were being added but never removed.
您看到的问题是窗口小部件已添加,但从未删除。 The following code could be simplified, but I tried to keep it close to yours so you could see the changes more easily.
以下代码可以简化,但我尝试使其与您的代码保持接近,以便您可以更轻松地看到更改。
The main change is now there is a class member screen_layout
and the widgets are added/removed from it inside actions()
. 现在的主要变化是,有一个类成员
screen_layout
并且在actions()
从其添加/删除了窗口小部件。
import sys
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QApplication
class Screen(QWidget):
def __init__(self):
super(Screen, self).__init__()
layout = QHBoxLayout(self)
self.screen_layout = layout
self.all_running()
layout.addWidget(self.running_full_widget)
self.actions_full_widget = None
self.actions('1')
# layout.addWidget(self.actions_full_widget)
self.setLayout(layout)
self.show()
def all_running(self):
self.running_full_widget = QWidget()
runnning_full_layout= QVBoxLayout()
button1 = QPushButton("btn1")
button2 = QPushButton("btn2")
button1.clicked.connect(lambda: self.actions('2'))
button2.clicked.connect(lambda: self.actions('3'))
runnning_full_layout.addWidget(button1)
runnning_full_layout.addWidget(button2)
self.running_full_widget.setLayout(runnning_full_layout)
def actions(self,value):
# Remove any previously added widget
if self.actions_full_widget is not None:
self.screen_layout.removeWidget(self.actions_full_widget)
self.actions_full_widget.deleteLater()
self.actions_full_widget= QWidget()
val = int(value)
print(val)
actions_layout = QVBoxLayout()
for i in range(val):
actions_item = QLabel(str(i))
actions_layout.addWidget(actions_item)
self.actions_full_widget.setLayout(actions_layout)
self.screen_layout.addWidget(self.actions_full_widget)
app = QApplication(sys.argv)
Gui = Screen()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.