-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshoppingcart_001.py
64 lines (55 loc) · 2.17 KB
/
shoppingcart_001.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import os
from PySide2 import QtGui,QtWidgets,QtCore
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import QUiLoader
cartItems= []
cartTotal=0
itemPrices = {"Shirt":200,"Jacket":700,"Socks":100,"Vest":150,"Trouser":500}
class Form(QObject):
def __init__(self, ui_file, parent = None):
super(Form,self).__init__(parent)
#load UI file
ui_file = QFile(ui_file)
ui_file.open(QFile.ReadOnly)
Loader = QUiLoader()
self.window = Loader.load(ui_file)
ui_file.close()
# initialize Ui buttons.etc
cmbItemList = self.window.findChild(QComboBox, "cmbItemList")
btnAdd = self.window.findChild(QPushButton,"btnAdd")
cmbItemList.addItem("Shirt")
cmbItemList.addItem("Vest")
cmbItemList.addItem("Trouser")
cmbItemList.addItem("Socks")
cmbItemList.addItem("Jacket")
#make clickable event on button
btnAdd.clicked.connect(self.updateCart)
self.window.show()
def addItem(self,ItemName):
# Add current item in comboBox to listview
lstViewCart = self.window.findChild(QListWidget, 'lstViewCart')
cmbItemList = self.window.findChild(QComboBox, "cmbItemList")
cartItems.append(ItemName)
lstViewCart.addItem(cmbItemList.currentText())
def updateCart(self):
global cartItems, cartTotal, itemPrices
#loop through all items in cart
for item in cartItems:
price = itemPrices.get(item, 0)
cartTotal+=price
lblTotal = self.window.findChild(QLabel, "lblTotal")
cmbItemList = self.window.findChild(QComboBox, "cmbItemList")
self.addItem(cmbItemList.currentText())
lblSummary = self.window.findChild(QLabel, 'lblSummary')
# Update cart summary
cartSummary = dict((item, cartItems.count(item)) for item in cartItems)
lblSummary.setText(str(cartSummary))
lblTotal.setText(str(cartTotal))
cartTotal=0
if __name__ == '__main__':
#LOAD UI
app = QApplication(sys.argv)
form = Form('cart.ui')
sys.exit(app.exec_())