在我的程序中,我从API获取数据。如果有人下令我想在我的窗口中看到这些数据而无需重新打开该应用程序。在这里,当我按下刷新按钮时,我想更新数据但声明了相同的数据,但是当我按下刷新按钮时,相同的数据被添加了两倍。下面给出的是我的示例代码,在这里我提到了字典来代替我的API。如何更新数据。
from PyQt4 import QtCore, QtGui
import sys, collections
from functools import partial
class InfoWidget(QtGui.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, info, parent=None):
super(InfoWidget, self).__init__(parent)
self._info = info
date_label = QtGui.QLabel("{} {}".format(info["Date"], info["Id"]), alignment= QtCore.Qt.AlignCenter, objectName="small")
amount_label = QtGui.QLabel("Amount:{}".format(info["Amount"]))
shopping_label = QtGui.QLabel("Shopping :{}".format(info["Shopping"]))
ordercity_label = QtGui.QLabel("Order City :{}".format(info["Order_City"]))
orderstate_label = QtGui.QLabel("Order State :{}".format(info["Order_State"]))
hline = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
lay = QtGui.QVBoxLayout(self)
lay.addWidget(date_label)
lay.addWidget(amount_label)
lay.addWidget(shopping_label)
lay.addWidget(ordercity_label)
lay.addWidget(orderstate_label)
lay.addWidget(hline)
@property
def info(self):
return self._info
def mousePressEvent(self, event):
self.clicked.emit()
super(InfoWidget, self).mousePressEvent(event)
class Orders(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Orders, self).__init__(parent)
self.Online_order_dict = [
{"Date" : "jan-24-2019", "Id" : "#175", "Amount" : "191 rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
{"Date" : "jan-25-2019", "Id" : "#186", "Amount" : "200 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
{"Date" : "jan-29-2019", "Id" : "#188", "Amount" : "250 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
{"Date" : "jan-25-2019", "Id" : "#176", "Amount" : "200 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
{"Date" : "jan-28-2019", "Id" : "#201", "Amount" : "250 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"}
]
self.qvw1 = QtGui.QWidget()
self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.scrollArea.setWidget(self.qvw1)
self.online_order_hbox = QtGui.QVBoxLayout(self.qvw1)
self.online_label = QtGui.QPushButton("Online Order")
self.refreshlabel = QtGui.QPushButton("Refresh")
self.refreshlabel.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
self.refreshlabel.clicked.connect(self.refresh_orders)
self.online_label.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
self.online_order_hbox.addWidget(self.online_label,QtCore.Qt.AlignTop)
self.online_order_hbox.addWidget(self.refreshlabel,QtCore.Qt.AlignBottom)
self.qvw1.setFixedWidth(550)
self.refresh_orders()
def refresh_orders(self):
for i in self.Online_order_dict:
w = InfoWidget(i)
w.clicked.connect(self.on_info_clicked)
self.online_order_hbox.addWidget(w)
self.setCentralWidget(self.scrollArea)
#self.Online_order_dict = self.result_data[1]
# for j in self.Online_order_dict:
# l=j['created'].split("T")
# self.online = {"Date": str(l[0]),"Id" : "#"+str(j['pk']),"Total Amount":str(j['totalAmount']),"Order City":j['billingCity'],"Order State":j['billingState']}
# onlineList.append(self.online)
#
# for index, i in enumerate(onlineList):
# w = InfoWidget1(index, i)
# w.clicked.connect(partial(self.on_info_clicked,"online"))
# self.online_order_hbox.addWidget(w)
@QtCore.pyqtSlot()
def on_info_clicked(self):
w = self.sender()
print(w.info)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
settingobj= Orders()
settingobj.showFullScreen()
sys.exit(app.exec_())
答案 0 :(得分:2)
在这种情况下,最简单的解决方案是在添加新组件之前删除InfoWidget,为此,我们可以通过findChildren()
方法获取它们:
def refresh_orders(self):
for w in self.online_order_hbox.parentWidget().findChildren(InfoWidget):
w.deleteLater()
for i in self.Online_order_dict:
w = InfoWidget(i)
w.clicked.connect(self.on_info_clicked)
self.online_order_hbox.addWidget(w)
self.setCentralWidget(self.scrollArea)