创建自定义计算器

2024-09-28 01:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个自定义计算器,它将获取输入参数,如输入价格、目标价格和止损值,然后计算预计利润和损失。一旦我让它运行良好并进行了一些优化,我想尝试添加一些功能,以便将数据放入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, 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行,我不确定)。我目前观察到的错误是,我的输入是非类型的,因此我无法对我输入的数字执行操作。确切的错误是:

projectedLoss_val = (EntryPrice_val * Amount_val) - (StopLoss_val * Amount_val)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

我想我只需要将值更改为浮点类型,否则我在创建和/或放置输入时会出错


Tags: textcolumnvalrootamountlabelgridrow
2条回答

下面是有效的代码(尽管存在被零除的问题):

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()

记住,每次调用函数时都必须有一种方法来检索信息

您正在使用网格变量来获取数据,而不是使用输入变量。您还需要使用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()

相关问题 更多 >

    热门问题