pyqt4没有更新布局和小部件

时间:2015-01-18 18:19:22

标签: python layout pyqt4 qwidget qlayout

我正在设计一个结帐窗口,用户点击按钮添加产品​​,按下按钮后,布局需要使用购物篮中的当前项目进行更新,我使用的是一个窗口分离器&我希望每次按下按钮时都会更新右侧,但出于某种原因!它不会更新,但产品会更新到购物篮词典!!

class Window(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.basket = []

        vbox = QtGui.QVBoxLayout(self)

        self.topleft = QtGui.QFrame(self)
        self.topleft.setFrameShape(QtGui.QFrame.StyledPanel)
        self.topleftfill()

        self.right = QtGui.QFrame(self)
        self.right.setFrameShape(QtGui.QFrame.StyledPanel)
        self.rightfill()

        self.bottomleft = QtGui.QFrame(self)
        self.bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)
        self.bottomleftfill()

        splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
        splitter1.addWidget(self.topleft)
        splitter1.addWidget(self.bottomleft)

        splitter2 = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(self.right)

        vbox.addWidget(splitter2)
        self.setLayout(vbox)
        splitter1.setSizes([100,620])
        splitter2.setSizes([960,360])


    def topleftfill(self):
        lineEdit = QtGui.QLineEdit()
        label = QtGui.QLabel('Search')
        label.setAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont("Unique", 50, QtGui.QFont.Bold)
        label.setFont(font)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(lineEdit)
        self.topleft.setLayout(layout)

    def bottomleftfill(self):        
        def producttypes():
            with sqlite3.connect('Example.db') as db:
                cursor = db.cursor()
                cursor.execute('select Type from ProductType')
                result = cursor.fetchall()
                return result

        def typeitems(itemtype):
            with sqlite3.connect('Example.db') as db:
                cursor = db.cursor()
                cursor.execute('select ProductTypeID from ProductType where Type=?', (itemtype,))
                typeid = cursor.fetchall()
                cursor.execute('select Name from Product where ProductTypeID=?',(typeid[0][0],))
                items = cursor.fetchall()
                return items

        typetab = QtGui.QTabWidget(self)
        types = producttypes()

        for name in types:
            tab = QtGui.QWidget()
            typetab.addTab(tab, name[0])
            products = typeitems(name[0])
            typetablayout = QtGui.QGridLayout()
            width = 5
            length = math.ceil(len(products)/5)
            item = 0
            for row in range(length):
                for column in range(width):
                    try:
                        button = QtGui.QPushButton(products[item][0])
                        button.setObjectName(products[item][0])
                        button.clicked.connect(self.addProduct)
                        typetablayout.addWidget(button, row, column)
                        item += 1
                    except IndexError:
                        break
            tab.setLayout(typetablayout)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(typetab)
        self.bottomleft.setLayout(layout)

    def rightfill(self):
        label = QtGui.QLabel('Basket')
        label.setAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont("Unique", 25, QtGui.QFont.Bold)
        label.setFont(font)
        row = 0
        itemslayout = QtGui.QGridLayout()
        for item in self.basket:
            label = QtGui.QLabel(item)
            print(item)
            itemslayout.addWidget(label, row, 3)
            row += 1

        layout = QtGui.QVBoxLayout()
        layout.addWidget(label)
        layout.addLayout(itemslayout)
        self.right.setLayout(layout)

    def addProduct(self): 
        product = self.sender().objectName()
        self.basket.append(product)
        basket = collections.Counter()
        for item in self.basket:
            basket[item] += 1
        self.rightfill()

0 个答案:

没有答案