我正在尝试编写一个自定义计算器,该计算器将接受输入参数,如入门价格、目标价格和止损值,然后计算预计利润和损失。一旦我让它运行得很好并进行了一点优化,我想尝试添加一些功能,将数据放入Excel或谷歌电子表格中,然而,我需要首先完成主要部分。我正在使用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, 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)
#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, 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)
#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, 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
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, 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():
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, 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)
#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()
记住,每次调用函数时,都必须有一种检索信息的方法