当点击首页中下单结算按钮后,就会进入商品的下单结算页面,该页面包含商品信息,订购取消、数量以及订购提示,最后的结算按钮。
效果如下图:
1 页面ui设计
我们在QTDesigner里设计出来面板如下:
其中用户、商品名、类别编号等是标签QLabel,订购、取消、结算、<< 、>>是按钮。数量是QSpinBox。他们每个组件的命名如下:
2 功能实现
接下来我们用代码进行功能实现。
2.1 初始化信息
首次进入下单结算的时候,会展示出第一个商品信息。
此时所使用的函数就是loadShop,并调用showCart来显示数据:
#加载已选/订购商品功能函数
def loadShop(self):
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
sheet3=book['商品表']
#1 到订单表中找到用户账号为当前用户userId且下单时间为空的记录的订单号
global oid, total
r = 0
for cell_uid in tuple(sheet2.columns)[1][1:]:
r +=1
if cell_uid.value == appvar.getID():
order = [cell.value for cell in tuple(sheet2.rows)[r]]
if order[3] == None:
oid = order[0]
#顺便读取支付金额(如果有的话)用于填写界面的“金额”栏
if order[2] != None:
total = order[2]
break
#2 根据第一步得到的订单号到订单项表中查询当前用户已选/订购的商品记录
global cart
cart=[]
list_key=['商品号','商品名称','类别编号','价格','数量','状态']
list_val=[]
pid =0
pname=''
tcode=''
pprice=0.00
cnum=0
status=''
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r +=1
if cell_oid.value == oid:
orderitem=[cell.value for cell in tuple(sheet1.rows)[r]]
pid=orderitem[1]
cnum=orderitem[2]
status=orderitem[3]
#根据商品号到商品表中进一步获取该商品的其他信息
n=0
for cell_pid in tuple(sheet3.columns)[0][1:]:
n +=1
if cell_pid.value == pid:
commodity=[cell.value for cell in tuple(sheet3.rows)[n]]
pname=commodity[2]
tcode=commodity[1]
pprice=commodity[3]
break
#填写list_val[],并与list_key[]合成为一个字典记录
list_val.append(pid)
list_val.append(pname)
list_val.append(tcode)
list_val.append(pprice)
list_val.append(cnum)
list_val.append(status)
dict_shop=dict(zip(list_key,list_val))
cart.append(dict_shop)
list_val=[]
pid=0
pname=''
tcode=''
pprice=0.00
cnum=0
status=''
if len(cart) != 0:
self.showCart() #显示数据
#显示数据
def showCart(self):
global index, total
self.lbPName.setText(cart[index]['商品名称'])
self.lbTCode.setText(cart[index]['类别编号'])
self.lbPrice.setText('%.2f' % cart[index]['价格'])
self.sbCNum.setValue(cart[index]['数量'])
image=QPixmap(r'image/goods/'+str(cart[index]['商品号'])+'.jpg')
self.lbImg.setPixmap(image)
if cart[index]['状态'] == '订购':
self.lbStatus.setText('已订购')
self.pbCfm.setEnabled(False)
self.pbCcl.setEnabled(True)
total = float(self.lbPrice.text()) * self.sbCNum.value()
else:
self.lbStatus.setText('')
self.pbCfm.setEnabled(True)
self.pbCcl.setEnabled(False)
total = float(self.lbPrice.text()) * 0
#显示底部金额栏
self.lbTotal.setText('%.2f'%total)
- 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.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
2.2 订购\取消
订购、取消按钮就在下图的地方,点击按钮触发订购,点击取消便取消订购。
其对应的逻辑代码如下:
# 订购
def cfmShop(self):
global cart, index, oid, total
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
#1 根据当前index从cart[]中读取商品号、价格,根据数量算出金额
pid=cart[index]['商品号']
cnum=self.sbCNum.value()
pay=cart[index]['价格'] * cnum
#2根据订单号oid和商品号在订单项表中定位,修改订单数量和状态
r=0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[1] == pid:
sheet1['C'+str(r+1)] = cnum
sheet1['D'+str(r+1)]='订购'
break
#3 根据订单号oid在订单表中定位,金额total累加之后更新支付金额
r=0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['C'+str(r+1)].alignment = Alignment(horizontal='center')
sheet2['D'+str(r+1)]=total + pay
break
book.save(r'data/netshop.xlsx')
book.close()
#4 刷新页面
self.loadShop()
#取消订购
def cancel(self):
global cart, index, oid, total
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
#1 根据当前的index从cart[]中读取 商品号 数量 价格 算出金额
pid = cart[index]['商品号']
cnum=cart[index]['数量']
pay=cart[index]['价格']*cnum
#根据订单号oid和商品号在订单表中定位,订货数量设置为1,修改状态
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[1] == pid:
sheet1['C'+str(r+1)] = 1
sheet1['D'+str(r+1)]='选购'
break
# 3 根据订单号oid在订单表中定位,金额total减去相应金额之后更新支付金额
r = 0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['C' + str(r + 1)].alignment = Alignment(horizontal='center')
sheet2['D' + str(r + 1)] = total - pay
break
book.save(r'data/netshop.xlsx')
book.close()
# 4 刷新页面
self.loadShop()
- 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.
- 65.
2.3 结算
订购完商品后,结算的金额会变成单价*数量的最终金额数,点击结算按钮就完成了结算功能。
#订单结算
def payOrder(self):
global cart, index, oid, total
book = openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1 = book['订单项表']
sheet2 = book['订单表']
sheet3 = book['商品表']
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[3] == '订购':
sheet1['D' + str(r + 1)] = '结算'
#更新商品表库存
n = 0
for cell_pid in tuple(sheet3.columns)[0][1:]:
n += 1
if cell_pid.value == orderitem[1]:
commodity = [cell.value for cell in tuple(sheet3.rows)[n]]
sheet3['E'+str(n+1)]==commodity[4]-orderitem[2]
r = 0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['D' + str(r + 1)].alignment = Alignment(horizontal='center')
sheet2['D' + str(r + 1)] = datetime.strftime(datetime.now(),'%Y.%m.%d %H:%M:%S')
break
list_oid = [cell.value for cell in tuple(sheet1.columns)[0][1:]]
oid_new=max(list_oid) +1
# 4 修改当前用户订单号oid尚未结算的记录为新订单号
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[3] == '结算':
sheet1['A' + str(r + 1)] = oid_new
#5 写入新的预备订单
oid=oid_new
s2r=str(sheet2.max_row +1)
sheet2['A' + s2r].alignment = Alignment(horizontal='center')
sheet2['A' + s2r] = oid
sheet2['B' + s2r] = appvar.getID()
book.save(r'data/netshop.xlsx')
book.close()
msgbox=QMessageBox.information(self,'提示','下单成功!')
print(msgbox)
#6刷新页面
index=0
total=0.00
self.loadShop()
- 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.
这样就完成了基本的功能。其他的部分功能,都不是主要的,参照完整代码实现。
3 完整代码
ui代码
# Form implementation generated from reading ui file 'ConfirmShop.ui'
#
# Created by: PyQt6 UI code generator 6.7.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QMainWindow
class Ui_MainWindow(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 500)
self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbTitle = QtWidgets.QLabel(parent=self.centralwidget)
self.lbTitle.setGeometry(QtCore.QRect(260, 30, 101, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.lbTitle.setFont(font)
self.lbTitle.setObjectName("lbTitle")
self.lbUsername = QtWidgets.QLabel(parent=self.centralwidget)
self.lbUsername.setGeometry(QtCore.QRect(50, 90, 54, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbUsername.setFont(font)
self.lbUsername.setObjectName("lbUsername")
self.lbUsr = QtWidgets.QLabel(parent=self.centralwidget)
self.lbUsr.setGeometry(QtCore.QRect(100, 90, 111, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbUsr.setFont(font)
self.lbUsr.setText("")
self.lbUsr.setObjectName("lbUsr")
self.pbCfm = QtWidgets.QPushButton(parent=self.centralwidget)
self.pbCfm.setGeometry(QtCore.QRect(440, 80, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.pbCfm.setFont(font)
self.pbCfm.setObjectName("pbCfm")
self.pbCcl = QtWidgets.QPushButton(parent=self.centralwidget)
self.pbCcl.setGeometry(QtCore.QRect(530, 80, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.pbCcl.setFont(font)
self.pbCcl.setObjectName("pbCcl")
self.scrollArea = QtWidgets.QScrollArea(parent=self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(40, 120, 571, 311))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 569, 309))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.lbgname = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbgname.setGeometry(QtCore.QRect(20, 20, 71, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbgname.setFont(font)
self.lbgname.setObjectName("lbgname")
self.lbPName = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbPName.setGeometry(QtCore.QRect(100, 20, 421, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbPName.setFont(font)
self.lbPName.setText("")
self.lbPName.setObjectName("lbPName")
self.lbbh = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbbh.setGeometry(QtCore.QRect(20, 70, 71, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbbh.setFont(font)
self.lbbh.setObjectName("lbbh")
self.lbTCode = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbTCode.setGeometry(QtCore.QRect(100, 70, 91, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbTCode.setFont(font)
self.lbTCode.setText("")
self.lbTCode.setObjectName("lbTCode")
self.lbjg = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbjg.setGeometry(QtCore.QRect(220, 70, 41, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbjg.setFont(font)
self.lbjg.setObjectName("lbjg")
self.lbPrice = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbPrice.setGeometry(QtCore.QRect(270, 70, 71, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbPrice.setFont(font)
self.lbPrice.setText("")
self.lbPrice.setObjectName("lbPrice")
self.lbsl = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbsl.setGeometry(QtCore.QRect(420, 70, 41, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbsl.setFont(font)
self.lbsl.setObjectName("lbsl")
self.sbCNum = QtWidgets.QSpinBox(parent=self.scrollAreaWidgetContents)
self.sbCNum.setGeometry(QtCore.QRect(470, 70, 61, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.sbCNum.setFont(font)
self.sbCNum.setObjectName("sbCNum")
self.lbimgname = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbimgname.setGeometry(QtCore.QRect(20, 130, 41, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbimgname.setFont(font)
self.lbimgname.setObjectName("lbimgname")
self.lbImg = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbImg.setGeometry(QtCore.QRect(200, 140, 141, 131))
self.lbImg.setText("")
self.lbImg.setObjectName("lbImg")
self.lbStatus = QtWidgets.QLabel(parent=self.scrollAreaWidgetContents)
self.lbStatus.setGeometry(QtCore.QRect(420, 250, 111, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbStatus.setFont(font)
self.lbStatus.setText("")
self.lbStatus.setObjectName("lbStatus")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.pbBack = QtWidgets.QPushButton(parent=self.centralwidget)
self.pbBack.setGeometry(QtCore.QRect(40, 440, 51, 24))
font = QtGui.QFont()
font.setPointSize(12)
self.pbBack.setFont(font)
self.pbBack.setObjectName("pbBack")
self.pbFor = QtWidgets.QPushButton(parent=self.centralwidget)
self.pbFor.setGeometry(QtCore.QRect(100, 440, 51, 24))
font = QtGui.QFont()
font.setPointSize(12)
self.pbFor.setFont(font)
self.pbFor.setObjectName("pbFor")
self.lbje = QtWidgets.QLabel(parent=self.centralwidget)
self.lbje.setGeometry(QtCore.QRect(330, 440, 41, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.lbje.setFont(font)
self.lbje.setObjectName("lbje")
self.lbTotal = QtWidgets.QLineEdit(parent=self.centralwidget)
self.lbTotal.setGeometry(QtCore.QRect(370, 440, 113, 20))
font = QtGui.QFont()
font.setPointSize(12)
self.lbTotal.setFont(font)
self.lbTotal.setObjectName("lbTotal")
self.pbPay = QtWidgets.QPushButton(parent=self.centralwidget)
self.pbPay.setGeometry(QtCore.QRect(510, 440, 75, 24))
font = QtGui.QFont()
font.setPointSize(12)
self.pbPay.setFont(font)
self.pbPay.setObjectName("pbPay")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "下单结算"))
self.lbTitle.setText(_translate("MainWindow", "下单结算"))
self.lbUsername.setText(_translate("MainWindow", "用户:"))
self.pbCfm.setText(_translate("MainWindow", "订购"))
self.pbCcl.setText(_translate("MainWindow", "取消"))
self.lbgname.setText(_translate("MainWindow", "商品名称"))
self.lbbh.setText(_translate("MainWindow", "类别编号"))
self.lbjg.setText(_translate("MainWindow", "价格"))
self.lbsl.setText(_translate("MainWindow", "数量"))
self.lbimgname.setText(_translate("MainWindow", "图片"))
self.pbBack.setText(_translate("MainWindow", "<<"))
self.pbFor.setText(_translate("MainWindow", ">>"))
self.lbje.setText(_translate("MainWindow", "金额"))
self.pbPay.setText(_translate("MainWindow", "结算"))
- 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.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
逻辑代码
# -*- coding:utf-8 -*-
"""
------------------------------------------------
File Name: ConfirmShop.py
Description:
Author: lzq
date:2024-08-08 08:43
------------------------------------------------
"""
import sys
from datetime import datetime
import openpyxl
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon, QPixmap
from PyQt6.QtWidgets import QApplication, QMessageBox
from openpyxl.styles import Alignment
from pyqt6Learn.myNetShop import appvar
#导入下单结算界面
from pyqt6Learn.myNetShop.ui.ConfirmShop_ui import Ui_MainWindow
#存放当前用户所有已选/订购的商品(字典列表)
cart=[]
index=0 #当前页显示的商品记录索引
oid=0 #当前用户的预备订单号
total=0.00 #当前用户已订购商品的总金额
class CfmWindow(Ui_MainWindow):
def __init__(self):
super(CfmWindow,self).__init__()
self.setupUi(self)
self.initUi()
def initUi(self):
self.setWindowIcon(QIcon('image/netshop.png'))
self.setWindowFlag(Qt.WindowType.MSWindowsFixedSizeDialogHint)
self.pbCfm.clicked.connect(self.cfmShop)
self.pbCcl.clicked.connect(self.cancel)
self.sbCNum.valueChanged.connect(self.changeCNum)
self.pbBack.clicked.connect(self.backward)
self.pbFor.clicked.connect(self.forward)
self.pbPay.clicked.connect(self.payOrder)
self.lbUsr.setText(appvar.getID())
#加载界面数据
self.loadShop()
#加载已选/订购商品功能函数
def loadShop(self):
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
sheet3=book['商品表']
#1 到订单表中找到用户账号为当前用户userId且下单时间为空的记录的订单号
global oid, total
r = 0
for cell_uid in tuple(sheet2.columns)[1][1:]:
r +=1
if cell_uid.value == appvar.getID():
order = [cell.value for cell in tuple(sheet2.rows)[r]]
if order[3] == None:
oid = order[0]
#顺便读取支付金额(如果有的话)用于填写界面的“金额”栏
if order[2] != None:
total = order[2]
break
#2 根据第一步得到的订单号到订单项表中查询当前用户已选/订购的商品记录
global cart
cart=[]
list_key=['商品号','商品名称','类别编号','价格','数量','状态']
list_val=[]
pid =0
pname=''
tcode=''
pprice=0.00
cnum=0
status=''
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r +=1
if cell_oid.value == oid:
orderitem=[cell.value for cell in tuple(sheet1.rows)[r]]
pid=orderitem[1]
cnum=orderitem[2]
status=orderitem[3]
#根据商品号到商品表中进一步获取该商品的其他信息
n=0
for cell_pid in tuple(sheet3.columns)[0][1:]:
n +=1
if cell_pid.value == pid:
commodity=[cell.value for cell in tuple(sheet3.rows)[n]]
pname=commodity[2]
tcode=commodity[1]
pprice=commodity[3]
break
#填写list_val[],并与list_key[]合成为一个字典记录
list_val.append(pid)
list_val.append(pname)
list_val.append(tcode)
list_val.append(pprice)
list_val.append(cnum)
list_val.append(status)
dict_shop=dict(zip(list_key,list_val))
cart.append(dict_shop)
list_val=[]
pid=0
pname=''
tcode=''
pprice=0.00
cnum=0
status=''
if len(cart) != 0:
self.showCart() #显示数据
#显示数据
def showCart(self):
global index, total
self.lbPName.setText(cart[index]['商品名称'])
self.lbTCode.setText(cart[index]['类别编号'])
self.lbPrice.setText('%.2f' % cart[index]['价格'])
self.sbCNum.setValue(cart[index]['数量'])
image=QPixmap(r'image/goods/'+str(cart[index]['商品号'])+'.jpg')
self.lbImg.setPixmap(image)
if cart[index]['状态'] == '订购':
self.lbStatus.setText('已订购')
self.pbCfm.setEnabled(False)
self.pbCcl.setEnabled(True)
total = float(self.lbPrice.text()) * self.sbCNum.value()
else:
self.lbStatus.setText('')
self.pbCfm.setEnabled(True)
self.pbCcl.setEnabled(False)
total = float(self.lbPrice.text()) * 0
#显示底部金额栏
self.lbTotal.setText('%.2f'%total)
def backward(self):
global index, cart
if len(cart) == 0:
pass
#如果是第一页,继续往前翻就返回最后一页
elif index == 0:
index =len(cart) - 1
self.showCart()
else:
index = index -1
self.showCart()
def forward(self):
global index, cart
if len(cart) == 0:
pass
elif index < len(cart):
self.showCart()
index =index +1
if index >=len(cart):
index =0
else:
index=0 #如果是最后一页,往后翻直接切换成第一页
self.showCart()
def cfmShop(self):
global cart, index, oid, total
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
#1 根据当前index从cart[]中读取商品号、价格,根据数量算出金额
pid=cart[index]['商品号']
cnum=self.sbCNum.value()
pay=cart[index]['价格'] * cnum
#2根据订单号oid和商品号在订单项表中定位,修改订单数量和状态
r=0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[1] == pid:
sheet1['C'+str(r+1)] = cnum
sheet1['D'+str(r+1)]='订购'
break
#3 根据订单号oid在订单表中定位,金额total累加之后更新支付金额
r=0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['C'+str(r+1)].alignment = Alignment(horizontal='center')
sheet2['D'+str(r+1)]=total + pay
break
book.save(r'data/netshop.xlsx')
book.close()
#4 刷新页面
self.loadShop()
#取消订购
def cancel(self):
global cart, index, oid, total
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1=book['订单项表']
sheet2=book['订单表']
#1 根据当前的index从cart[]中读取 商品号 数量 价格 算出金额
pid = cart[index]['商品号']
cnum=cart[index]['数量']
pay=cart[index]['价格']*cnum
#根据订单号oid和商品号在订单表中定位,订货数量设置为1,修改状态
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[1] == pid:
sheet1['C'+str(r+1)] = 1
sheet1['D'+str(r+1)]='选购'
break
# 3 根据订单号oid在订单表中定位,金额total减去相应金额之后更新支付金额
r = 0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['C' + str(r + 1)].alignment = Alignment(horizontal='center')
sheet2['D' + str(r + 1)] = total - pay
break
book.save(r'data/netshop.xlsx')
book.close()
# 4 刷新页面
self.loadShop()
def changeCNum(self):
global cart, index, oid, total
book=openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1 = book['订单项表']
sheet2 = book['订单表']
# 1 根据当前的index从cart[]中读取 商品号 数量(调整前) 价格 状态 从sbCNum获取数量,根据价格和数量(调整后)算出要加的金额
status = cart[index]['状态']
if status != '订购':
pass
else:
pid = cart[index]['商品号']
cnum_old = cart[index]['数量']
cnum_new=self.sbCNum.value()
pay_add = cart[index]['价格'] * (cnum_new - cnum_old)
# 根据订单号oid和商品号在订单表中定位,修改订货数量
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[1] == pid:
sheet1['C' + str(r + 1)] = cnum_new
break
# 3 根据订单号oid在订单表中定位,金额total减去相应金额之后更新支付金额
r = 0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['C' + str(r + 1)].alignment = Alignment(horizontal='center')
sheet2['C' + str(r + 1)] = total + pay_add
break
book.save(r'data/netshop.xlsx')
book.close()
# 4 刷新页面
self.loadShop()
def payOrder(self):
global cart, index, oid, total
book = openpyxl.load_workbook(r'data/netshop.xlsx')
sheet1 = book['订单项表']
sheet2 = book['订单表']
sheet3 = book['商品表']
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[3] == '订购':
sheet1['D' + str(r + 1)] = '结算'
#更新商品表库存
n = 0
for cell_pid in tuple(sheet3.columns)[0][1:]:
n += 1
if cell_pid.value == orderitem[1]:
commodity = [cell.value for cell in tuple(sheet3.rows)[n]]
sheet3['E'+str(n+1)]==commodity[4]-orderitem[2]
r = 0
for cell_oid in tuple(sheet2.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
sheet2['D' + str(r + 1)].alignment = Alignment(horizontal='center')
sheet2['D' + str(r + 1)] = datetime.strftime(datetime.now(),'%Y.%m.%d %H:%M:%S')
break
list_oid = [cell.value for cell in tuple(sheet1.columns)[0][1:]]
oid_new=max(list_oid) +1
# 4 修改当前用户订单号oid尚未结算的记录为新订单号
r = 0
for cell_oid in tuple(sheet1.columns)[0][1:]:
r += 1
if cell_oid.value == oid:
orderitem = [cell.value for cell in tuple(sheet1.rows)[r]]
if orderitem[3] == '结算':
sheet1['A' + str(r + 1)] = oid_new
#5 写入新的预备订单
oid=oid_new
s2r=str(sheet2.max_row +1)
sheet2['A' + s2r].alignment = Alignment(horizontal='center')
sheet2['A' + s2r] = oid
sheet2['B' + s2r] = appvar.getID()
book.save(r'data/netshop.xlsx')
book.close()
msgbox=QMessageBox.information(self,'提示','下单成功!')
print(msgbox)
#6刷新页面
index=0
total=0.00
self.loadShop()
# if __name__=='__main__':
# app=QApplication(sys.argv)
# mainwindow=CfmWindow()
# mainwindow.show()
# sys.exit(app.exec())
- 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.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.