Python:成本核算计算器输出

时间:2018-11-22 12:26:22

标签: python python-3.x tkinter

大家好!

我正忙于为标牌部门创建一个小型的成本计算器。 我没有让计算器输出金额。

简要说明: 输入高度和宽度,然后按回车键以显示成本。

我如何使其工作?任何建议,请谢谢。

from tkinter import *
from tkinter import ttk

#Define the Functions here
def squeare(height,width):
    cost = ((float(height) * float(width))/1000000 * 650 * 1.15 * 1.50)
    return cost

window = Tk()
window.title("Costing Calculator V1.0")

mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)

height = StringVar()
width = StringVar()

#ttk.Label(mainframe, text="H").grid(column=1, row=1, sticky=E)
height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=3, row=1, sticky=(W,E))

ttk.Label(mainframe, text="X").grid(column=5, row=1, sticky=E)

#ttk.Label(mainframe, text="W").grid(column=6, row=1, sticky=E)
width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=7, row=1, sticky=(W,E))

ttk.Label(mainframe, text="=").grid(column=8, row=1, sticky=E)

#call function
#squeare()

window.mainloop()

2 个答案:

答案 0 :(得分:0)

用于捕获第二个输入字段的“ enter”事件。

from tkinter import *
from tkinter import ttk

#Define the Functions here
def squeare(height,width):
    cost = ((float(height) * float(width))/1000000 * 650 * 1.15 * 1.50)
    result.configure(text=str(cost))


def enter(event=None):
    squeare(height.get(), width.get())

window = Tk()
window.title("Costing Calculator V1.0")

mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)

height = StringVar()
width = StringVar()

#ttk.Label(mainframe, text="H").grid(column=1, row=1, sticky=E)
height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=3, row=1, sticky=(W,E))

ttk.Label(mainframe, text="X").grid(column=5, row=1, sticky=E)

#ttk.Label(mainframe, text="W").grid(column=6, row=1, sticky=E)
width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=7, row=1, sticky=(W,E))

width_entry.bind('<Return>',enter)

ttk.Label(mainframe, text="=").grid(column=8, row=1, sticky=E)
result = ttk.Label(mainframe, text="")
result.grid(column=9, row=1, sticky=E)

window.mainloop()

答案 1 :(得分:0)

我用回车键把窗框装订了。没有一个 result 标签。每当您按下Enter键时,其文本都会被更新。

from tkinter import *
from tkinter import ttk

def squeare(height, width):
    cost = float(height) * float(width)/1000000 * 650 * 1.15 * 1.50
    result.configure(text=str(cost))

window = Tk()
window.title("Costing Calculator V1.0")

mainframe = ttk.Frame(window, padding="20 20 20 20")
mainframe.grid(column=0, row=0, sticky="nsew")
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)

height = StringVar()
height.set(0)
width = StringVar()
width.set(0)

height_entry = ttk.Entry(mainframe, width=7, textvariable=height)
height_entry.grid(column=0, row=0, sticky="we")

ttk.Label(mainframe, text="X").grid(column=1, row=0, sticky="e")

width_entry = ttk.Entry(mainframe, width=7, textvariable=width)
width_entry.grid(column=2, row=0, sticky=(W,E))

ttk.Label(mainframe, text="=").grid(column=3, row=0, sticky="e")
result = ttk.Label(mainframe)
result.grid(row=0, column=4)

window.bind('<Return>', lambda e: squeare(height.get(), width.get()))

window.mainloop()

enter image description here