2021.1.13做的星露谷作物计算器,初步只完成了对excel表内数据和图表的生成,交互也只是input(),正好2021.1.14初步学习了easygui相关的知识,学以致用,生成一个简单的gui可交互界面,对excel表和代码也进行了部分修改,以适应蔓越莓,蓝莓等一次采摘掉落好几个果子的这种作物的计算
2021.1.13
让用户选择季节,输入当前的天数,然后就可以在excel里查看相应的数据和图表
和上次的表格相比,去掉了一些没用的列
#加载相关模块
import xlwings as xw
import matplotlib.pyplot as plt
import pandas as pd
import easygui as g
app = xw.App(visible=False, add_book=False)
file_path = 'd:\\python_study\\xinglugu.xlsx' #定义文件路径
workbook = app.books.open(file_path)
wsh1 = workbook.sheets['spring'] #定义需要用的工作表
wsh2 = workbook.sheets['summer']
wsh3 = workbook.sheets['autumn']
wsh4 = workbook.sheets['special']
season = g.buttonbox(msg="目前的季节是?",title="季节",choices=("春","夏","秋")) #生成选项gui
day = g.integerbox(msg="请输入目前的天数",title="天数",lowerbound=1,upperbound=28)#生成输入框gui
if season == "春":
i = 2 #公式需要循环获得每个单元格的值,定义各个变量,注意此处用f-string方式格式化变量,否则会报错
while wsh1.range(f"B{i}").value is not None: #此处意指判定B列值为空时停止循环,注意判定为空值用None
Bi = wsh1.range(f"B{i}").value #定义变量
Ci = wsh1.range(f"C{i}").value
Fi = wsh1.range(f"F{i}").value
Gi = wsh1.range(f"G{i}").value
Hi = wsh1.range(f"H{i}").value
if day + int(Gi) > 28: #用if语句判定如果目前天数加上作物生长需要天数大于28,则收益为0
wsh1.range(f"L{i}").value = "0"
else:
if Hi == None: #用if语句判定是否为可持续收获作物
a = (Ci*Fi-Bi)/Gi
else:
a = (int(((28-day)-Gi)/Hi+1) *Ci*Fi -Bi)/(28-day)
wsh1.range(f"L{i}").value = a
i +=1
workbook.save()
df = pd.read_excel(file_path,sheet_name='spring') #用pandas读取'spring'工作表的数据
figure = plt.figure() #画图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = df['作物名称'] #以'作物名称'列为x轴
y = df['日均收益'] #以'日均收益'列为x轴
plt.bar(x,y,color = 'black')
#判定当工作表里有图片时删除该图片,否则会导致每次运行程序重新计算添加图片都会多一张,太乱
while wsh1.pictures:
wsh1.pictures[0].delete() #翻阅xlwings 手册翻到的代码,这块我搜了半天都没找到怎么执行
wsh1.pictures.add(figure,left = 500,top = 200)
if season == "夏":
i = 2 #公式需要循环获得每个单元格的值,定义各个变量,注意此处用f-string方式格式化变量,否则会报错
while wsh2.range(f"B{i}").value is not None: #此处意指判定B列值为空时停止循环,注意判定为空值用None
Bi = wsh2.range(f"B{i}").value #定义变量
Ci = wsh2.range(f"C{i}").value
Fi = wsh2.range(f"F{i}").value
Gi = wsh2.range(f"G{i}").value
Hi = wsh2.range(f"H{i}").value
if day + int(Gi) > 28: #用if语句判定如果目前天数加上作物生长需要天数大于28,则收益为0
wsh2.range(f"L{i}").value = "0"
else:
if Hi == None: #用if语句判定是否为可持续收获作物
a = (Ci*Fi-Bi)/Gi
else:
a = (int(((28-day)-Gi)/Hi+1) *Ci*Fi -Bi)/(28-day)
wsh2.range(f"L{i}").value = a
i +=1
workbook.save()
df = pd.read_excel(file_path,sheet_name='summer') #用pandas读取'spring'工作表的数据
figure = plt.figure(figsize=(10, 6.5)) #画图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = df['作物名称'] #以'作物名称'列为x轴
y = df['日均收益'] #以'日均收益'列为x轴
plt.bar(x,y,color = 'black')
#判定当工作表里有图片时删除该图片,否则会导致每次运行程序重新计算添加图片都会多一张,太乱
while wsh2.pictures:
wsh2.pictures[0].delete() #翻阅xlwings 手册翻到的代码,这块我搜了半天都没找到怎么执行
wsh2.pictures.add(figure,left = 500,top = 200)
if season == "秋":
i = 2 #公式需要循环获得每个单元格的值,定义各个变量,注意此处用f-string方式格式化变量,否则会报错
while wsh3.range(f"B{i}").value is not None: #此处意指判定B列值为空时停止循环,注意判定为空值用None
Bi = wsh3.range(f"B{i}").value #定义变量
Ci = wsh3.range(f"C{i}").value
Fi = wsh3.range(f"F{i}").value
Gi = wsh3.range(f"G{i}").value
Hi = wsh3.range(f"H{i}").value
if day + int(Gi) > 28: #用if语句判定如果目前天数加上作物生长需要天数大于28,则收益为0
wsh3.range(f"L{i}").value = "0"
else:
if Hi == None: #用if语句判定是否为可持续收获作物
a = (Ci*Fi-Bi)/Gi
else:
a = (int(((28-day)-Gi)/Hi+1) *Ci*Fi -Bi)/(28-day)
wsh3.range(f"L{i}").value = a
i +=1
workbook.save()
df = pd.read_excel(file_path,sheet_name='autumn') #用pandas读取'spring'工作表的数据
figure = plt.figure() #画图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = df['作物名称'] #以'作物名称'列为x轴
y = df['日均收益'] #以'日均收益'列为x轴
plt.bar(x,y,color = 'black')
#判定当工作表里有图片时删除该图片,否则会导致每次运行程序重新计算添加图片都会多一张,太乱
while wsh3.pictures:
wsh3.pictures[0].delete() #翻阅xlwings 手册翻到的代码,这块我搜了半天都没找到怎么执行
wsh3.pictures.add(figure,left = 500,top = 200)
workbook.save()
workbook.close()
app.quit()
1.在写根据用户选择判断季节调用工作表时应用if,当时错用了while,导致无限循环,低级错误
2.夏季作物太多,生成的图表字都挤到一起去了,查询了一下,可以用plt.figure(figsize=(10, 6.5))该代码调整图片的大小,将图片长宽拉到合适的值后字就正常了