如何在for循环的每次迭代中获取字典中的所有乘积

时间:2019-02-01 04:16:44

标签: python-2.7 pyqt4

这是我的示例代码,我想在窗口小部件中显示我的所有产品,因此我在for循环中将其作为字典使用,但是我没有在字典中获取所有产品,最后一个产品覆盖了前一个产品。请问任何一个可以帮助我做错了的地方。谢谢您。 下面是我的代码:

products_list = {"Givers":{"price": 140,"Quantity":100,"Discount":0,"Total_price":14000,"Paid_amount":14000},"New_Member":{"price": 1200,"Quantity":10,"Discount":0,"Total_price":12000,"Paid_amount":12000},"YearMembers_Ribbons":{"price": 110,"Quantity":0,"Discount":0,"Total_price":1100,"Paid_amount":1100}}
for key,value in products_list.items():
    self.widget_orders_info = QtGui.QWidget()
    self.widget_orders_info.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    self.widget_orders_info.setStyleSheet("QWidget {background-color: blue;color: white;}")
    self.order_info_vbox = QtGui.QVBoxLayout(self.widget_orders_info)
    self.title_hbox = QtGui.QHBoxLayout()
    self.orderdetail_label = QtGui.QLabel("Order_Details(22HRS)",alignment =QtCore.Qt.AlignCenter,objectName="small")
    self.close_pushbtn = QtGui.QPushButton("Close")
    self.title_hbox.addWidget(self.orderdetail_label)
    self.title_hbox.addStretch()
    self.title_hbox.addWidget(self.close_pushbtn)
    self.close_pushbtn.clicked.connect(self.widget_orders_info.close)
    self.order_info_vbox.addLayout(self.title_hbox)
    self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
    self.order_info_vbox.addWidget(self.line)
    self.order_hbox = QtGui.QHBoxLayout()
    keys = value.keys()
    values = value.values()
    self.line1 = QtGui.QFrame(frameShape=QtGui.QFrame.VLine)
    self.order_hbox.addWidget(self.line1)
    self.items_Vboxlay = QtGui.QVBoxLayout()
    self.vbox2 = QtGui.QVBoxLayout()
    self.item_label = QtGui.QLabel(str(key), objectName="small")
    self.vbox2.addWidget(self.item_label)
    self.hbox1 = QtGui.QHBoxLayout()
    self.price = QtGui.QLabel(keys[1]+": " +str(values[1]))
    self.qnty =QtGui.QLabel(keys[4]+": "+str(values[4]))
    self.discount = QtGui.QLabel(keys[0]+ ": " +u'\u20B9 '+ str(values[0]))
    self.totalprice =QtGui.QLabel(keys[2]+ ": "+u'\u20B9 '+ str(values[2]))
    self.paid = QtGui.QLabel(keys[3] + ": "+u'\u20B9 '+ str(values[3]))
    self.hbox1.addWidget(self.price)
    self.hbox1.addWidget(self.qnty)
    self.hbox1.addWidget(self.discount)
    self.hbox1.addWidget(self.totalprice)
    self.hbox1.addWidget(self.paid)
    self.line4 = QtGui.QFrame();self.line4.setFrameShape(QtGui.QFrame.HLine)
    self.vbox2.addLayout(self.hbox1)
    self.vbox2.addLayout(self.h2box)
    self.vbox2.addWidget(self.line1)
self.items_Vboxlay.addLayout(self.vbox2)
self.order_hbox.addLayout(self.items_Vboxlay)
self.order_info_vbox.addLayout(self.order_hbox)
self.add_hbox = QtGui.QHBoxLayout()
self.print_btn = QtGui.QPushButton("Print")
self.print_btn.clicked.connect(partial(self.print_function,type))
self.add_hbox.setAlignment(QtCore.Qt.AlignCenter)
self.add_hbox.addWidget(self.print_btn)
self.order_info_vbox.addLayout(self.add_hbox)
self.widget_orders_info.show()
self.widget_orders_info.resize(500,300)

1 个答案:

答案 0 :(得分:0)

抱歉,我有Python-3PyQt5。我认为我的榜样应该会对您有所帮助。

import sys
from functools import partial
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *


class Window(QMainWindow):  
    def __init__(self):
        super().__init__()

        products_list = {
            "Givers":{
                "price":        140,
                "Quantity":     100,
                "Discount":       0,
                "Total_price":14000,
                "Paid_amount":14000},
            "New_Member":{
                "price":       1200,
                "Quantity":      10,
                "Discount":       0,
                "Total_price":12000,
                "Paid_amount":12000},
            "YearMembers_Ribbons":{
                "price":       110,
                "Quantity":      0,
                "Discount":      0,
                "Total_price":1100,
                "Paid_amount":1100}
        }
# *1 +++
        self.widget_orders_info = QWidget()
        self.widget_orders_info.setWindowFlags(Qt.FramelessWindowHint)
        self.widget_orders_info.setStyleSheet("QWidget {background-color: blue;color: white;}")

        self.order_info_vbox = QVBoxLayout(self.widget_orders_info)

        self.title_hbox = QHBoxLayout()
        self.orderdetail_label = QLabel("Order_Details(22HRS)",
                                        alignment=Qt.AlignCenter,
                                        objectName="small")
        self.close_pushbtn = QPushButton("Close")
        self.title_hbox.addWidget(self.orderdetail_label)
        self.title_hbox.addStretch()
        self.title_hbox.addWidget(self.close_pushbtn)
        self.close_pushbtn.clicked.connect(self.widget_orders_info.close)

        self.order_info_vbox.addLayout(self.title_hbox)
        self.line = QFrame(frameShape=QFrame.HLine)
        self.order_info_vbox.addWidget(self.line)

        self.order_hbox = QHBoxLayout()   

# *2 +++
        self.line1 = QFrame(frameShape = QFrame.VLine)
        self.order_hbox.addWidget(self.line1)
        self.items_Vboxlay = QVBoxLayout()
        self.vbox2         = QVBoxLayout()

        for key, value in products_list.items():

#            self.widget_orders_info = QWidget()
#            ...
# transferred from the `for` loop, see `* 1 +++`
#            ...  
#            self.order_hbox = QHBoxLayout()


#            keys   = value.keys()
#            values = value.values()
            keys   = [ vk for vk in value.keys()]
            values = [ vv for vv in value.values()]

#            self.line1 = QFrame(frameShape = QFrame.VLine)
#            ...
# transferred from the `for` loop, see `* 2 +++`
#            ...  
#            self.vbox2 = QVBoxLayout()


            self.item_label = QLabel(str(key), objectName="small")
            self.vbox2.addWidget(self.item_label)

            self.hbox1 = QHBoxLayout()

            self.price      = QLabel(keys[1] + ": " + str(values[1]))
            self.qnty       = QLabel(keys[4] + ": " + str(values[4]))
            self.discount   = QLabel(keys[0] + ": " + u'\u20B9 ' + str(values[0]))
            self.totalprice = QLabel(keys[2] + ": " + u'\u20B9 ' + str(values[2]))
            self.paid       = QLabel(keys[3] + ": " + u'\u20B9 ' + str(values[3]))

            self.hbox1.addWidget(self.price)
            self.hbox1.addWidget(self.qnty)
            self.hbox1.addWidget(self.discount)
            self.hbox1.addWidget(self.totalprice)
            self.hbox1.addWidget(self.paid)

            self.line4 = QFrame()
            self.line4.setFrameShape(QFrame.HLine)

            self.vbox2.addLayout(self.hbox1)
#            self.vbox2.addLayout(self.h2box)    # ??????????????????????????
            self.vbox2.addWidget(self.line1)

        self.items_Vboxlay.addLayout(self.vbox2)
        self.order_hbox.addLayout(self.items_Vboxlay)
        self.order_info_vbox.addLayout(self.order_hbox)
        self.add_hbox  = QHBoxLayout()
        self.print_btn = QPushButton("Print")
        self.print_btn.clicked.connect(partial(self.print_function, type))
        self.add_hbox.setAlignment(Qt.AlignCenter)
        self.add_hbox.addWidget(self.print_btn)
        self.order_info_vbox.addLayout(self.add_hbox)
        self.widget_orders_info.show()
        self.widget_orders_info.resize(500,300)

    def print_function(self, t):
        print("--- t ->", t)

if __name__ == '__main__':  
    import sys
    app = QApplication(sys.argv)
    w = Window()
#    w.show()
    sys.exit(app.exec_())

enter image description here