如何解决创建自定义计算器
我正在尝试编写一个自定义计算器,该计算器将采用输入参数(例如入场价、目标价和止损值),然后计算预计盈亏。一旦我让它运行良好并对其进行了一些优化,我想尝试添加可以将数据放入 Excel 或 Google 电子表格的功能,但是,我需要先让主要部分工作。我正在使用 tkinter,以便我可以创建 GUI。
我对编码非常陌生,这就是我迄今为止设法做到的:
from tkinter import *
root = Tk()
#Create labels for each parameter
label_Amount = Label(root,text="Amount:")
label_StopLoss = Label(root,text="Stop loss:")
label_EntryPrice = Label(root,text="Entry price:")
label_TargetPrice = Label(root,text="Target price:")
label_ProjectedLoss = Label(root,text="Projected loss:")
label_PLRatio = Label(root,text="P/L Ratio:")
label_ProjectedProfit = Label(root,text="Projected profit:")
#Create inputs
entry_Amount = Entry(root)
entry_StopLoss = Entry(root)
entry_EntryPrice = Entry(root)
entry_TargetPrice = Entry(root)
#Place labels
label_Amount.grid(row=0,column=2)
label_StopLoss.grid(row=1,column= 0)
label_ProjectedLoss.grid(row=2,column=0)
label_EntryPrice.grid(row=1,column=2)
label_PLRatio.grid(row=2,column=2)
label_TargetPrice.grid(row=1,column=4)
label_ProjectedProfit.grid(row=2,column=4)
#Place inputs
Amount_val = entry_Amount.grid(row=0,column=3)
StopLoss_val = entry_StopLoss.grid(row=1,column=1)
EntryPrice_val = entry_EntryPrice.grid(row=1,column=3)
TargetPrice_val = entry_TargetPrice.grid(row=1,column=5)
#Calculation long function
def calculateLong():
projectedLoss_val = (EntryPrice_val * Amount_val) - (StopLoss_val * Amount_val)
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (TargetPrice_val * Amount_val) - (EntryPrice_val * Amount_val)
label_ProjectedProfit_val = Label(root,text=str(projectedProfit_val))
PLRatio_val = projectedProfit_val / projectedLoss_val
label_PLRatio_val = Label(root,text=str(PLRatio_val))
label_ProjectedLoss_val.grid(row=2,column=1)
label_ProjectedProfit_val.grid(row=2,column=5)
label_PLRatio_val.grid(row=2,column=3)
#Calcualtion short function
def calculateShort():
projectedLoss_val = (StopLoss_val * Amount_val) - (EntryPrice_val * Amount_val)
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (EntryPrice_val * Amount_val) - (TargetPrice_val * Amount_val)
label_ProjectedProfit_val = Label(root,column=3)
#Create long and short buttons that trigger functions
button_long = Button(root,text="Calculate long",bg="green",command=calculateLong)
button_short = Button(root,text="Calculate short",bg="red",command=calculateShort)
#Position the buttons to either side of Amount
button_long.grid(row=0,column=4,columnspan=2)
button_short.grid(row=0,column=0,columnspan=2)
root.mainloop()
我认为我目前的错误在于我的输入(第 14 行或第 29 行,我不确定)。我目前观察到的错误表明我的输入是 nonetype,因此我无法对输入的数字执行操作。确切的错误是:
projectedLoss_val = (EntryPrice_val * Amount_val) - (StopLoss_val * Amount_val)
TypeError: unsupported operand type(s) for *: 'nonetype' and 'nonetype'
我想我只需要将值更改为浮点类型,或者我在输入的创建和/或放置方面做错了。
解决方法
您正在使用网格变量而不是使用输入变量来获取数据。您还需要使用 get()
来获取数据。您以字符串格式获取数据,因此需要将其转换为 int 或 float。
from tkinter import *
root = Tk()
#Create labels for each parameter
label_Amount = Label(root,text="Amount:")
label_StopLoss = Label(root,text="Stop loss:")
label_EntryPrice = Label(root,text="Entry price:")
label_TargetPrice = Label(root,text="Target price:")
label_ProjectedLoss = Label(root,text="Projected loss:")
label_PLRatio = Label(root,text="P/L Ratio:")
label_ProjectedProfit = Label(root,text="Projected profit:")
#Create inputs
entry_Amount = Entry(root)
entry_StopLoss = Entry(root)
entry_EntryPrice = Entry(root)
entry_TargetPrice = Entry(root)
#Place labels
label_Amount.grid(row=0,column=2)
label_StopLoss.grid(row=1,column= 0)
label_ProjectedLoss.grid(row=2,column=0)
label_EntryPrice.grid(row=1,column=2)
label_PLRatio.grid(row=2,column=2)
label_TargetPrice.grid(row=1,column=4)
label_ProjectedProfit.grid(row=2,column=4)
#Place inputs
Amount_val = entry_Amount.grid(row=0,column=3)
StopLoss_val = entry_StopLoss.grid(row=1,column=1)
EntryPrice_val = entry_EntryPrice.grid(row=1,column=3)
TargetPrice_val = entry_TargetPrice.grid(row=1,column=5)
#Calculation long function
def calculateLong():
projectedLoss_val = (int(entry_EntryPrice.get()) * int(entry_Amount.get())) - (int(entry_StopLoss.get()) * int(entry_Amount.get()))
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (int(entry_TargetPrice.get()) * int(entry_Amount.get())) - (int(entry_EntryPrice.get()) * int(entry_Amount.get()))
label_ProjectedProfit_val = Label(root,text=str(projectedProfit_val))
PLRatio_val = projectedProfit_val / projectedLoss_val
label_PLRatio_val = Label(root,text=str(PLRatio_val))
label_ProjectedLoss_val.grid(row=2,column=1)
label_ProjectedProfit_val.grid(row=2,column=5)
label_PLRatio_val.grid(row=2,column=3)
#Calcualtion short function
def calculateShort():
projectedLoss_val = (int(entry_StopLoss.get()) * int(entry_Amount.get())) - (int(entry_EntryPrice.get()) * int(entry_Amount.get()))
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (int(entry_EntryPrice.get()) * int(entry_Amount.get())) - (int(entry_TargetPrice.get()) * int(entry_Amount.get()))
label_ProjectedProfit_val = Label(root,column=3)
#Create long and short buttons that trigger functions
button_long = Button(root,text="Calculate long",bg="green",command=calculateLong)
button_short = Button(root,text="Calculate short",bg="red",command=calculateShort)
#Position the buttons to either side of Amount
button_long.grid(row=0,column=4,columnspan=2)
button_short.grid(row=0,column=0,columnspan=2)
root.mainloop()
,
所以这是有效的代码(尽管除以零存在问题):
from tkinter import *
root = Tk()
#Create labels for each parameter
label_Amount = Label(root,column=4)
#Place inputs
entry_Amount.grid(row=0,column=3)
entry_StopLoss.grid(row=1,column=1)
entry_EntryPrice.grid(row=1,column=3)
entry_TargetPrice.grid(row=1,column=5)
#Calculation long function
def calculateLong():
global entry_Amount,entry_StopLoss,entry_EntryPrice,entry_TargetPrice
Amount_val = int(entry_Amount.get())
EntryPrice_val = int(entry_EntryPrice.get())
StopLoss_val = int(entry_StopLoss.get())
TargetPrice_val = int(entry_TargetPrice.get())
projectedLoss_val = (EntryPrice_val * Amount_val) - (StopLoss_val * Amount_val)
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (TargetPrice_val * Amount_val) - (EntryPrice_val * Amount_val)
label_ProjectedProfit_val = Label(root,column=3)
#Calcualtion short function
def calculateShort():
StopLoss_val = int(entry_StopLoss.get())
Amount_val = int(entry_Amount.get())
EntryPrice_val = int(entry_EntryPrice.get())
TargetPrice_val = int(entry_TargetPrice.get())
projectedLoss_val = (StopLoss_val * Amount_val) - (EntryPrice_val * Amount_val)
label_ProjectedLoss_val = Label(root,text=str(projectedLoss_val))
projectedProfit_val = (EntryPrice_val * Amount_val) - (TargetPrice_val * Amount_val)
label_ProjectedProfit_val = Label(root,columnspan=2)
root.mainloop()
记住,每次调用函数时都必须有一种方法来检索信息
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。