将文本添加到显示变量的Label

时间:2017-06-21 05:36:23

标签: python-3.x tkinter

我正在制定一个程序来计算某人会为报价收取多少费用,到目前为止,我已将其付诸实施,但是底部有一个表格,其中包含所有值,但不清楚到期它没有被标记为每行中的值。

我如何将标签放在那里?

from tkinter import *


app = Tk()


woodType = {'Ash': 3562.39, 'Maple': 3554.01, 'Beech': 2661.32, 'Cherry': 
4887.15}  
choice_var = StringVar()
dimensions = DoubleVar()
employ = DoubleVar()
length = DoubleVar()
pay = DoubleVar()


def math():
    D = dimensions.get()
    choice = choice_var.get()
    money = pay.get()
    employee = employ.get()
    time = length.get()

    if choice == 'Ash':
        woodPrice = woodType['Ash'] * D
    if choice == 'Maple':
        woodPrice = woodType['Maple'] * D
    if choice == 'Beech':
        woodPrice = woodType['Beech'] * D
    if choice == 'Cherry':
        woodPrice = woodType['Cherry'] * D

    smallMaterials = 10/100* woodPrice

    totalMaterialCost = woodPrice+smallMaterials

    x = money*time
    totalEmployeePay = x*employee

    Price = totalMaterialCost+totalEmployeePay

    GST = 20/100*totalMaterialCost

    Overhead = 10/100 * Price

    QuotePrice= Price+GST+Overhead

    display(woodPrice, smallMaterials, totalMaterialCost, totalEmployeePay, Price,GST, Overhead,QuotePrice)

def saving():
    print('waiting...')

choice_var.set('Select wood type')

Label(app, text='QUOTE CALCULATOR').grid(row=0, column=1, sticky=NSEW)
OptionMenu(app, choice_var, 'Ash', 'Maple', 'Beech', 'Cherry').grid(row=1, column=1, sticky=NSEW)

Label(app, text='Enter Dimensions of the wood (in cubic meters or gallons)').grid(row=2, column=1, sticky=NSEW)
Entry(app, textvariable=dimensions).grid(row=3, column=1, sticky=NSEW) 

Label(app, text='Enter amount of employees on project').grid(row=4, column=1, sticky=NSEW)
Entry(app, textvariable=employ).grid(row=5, column=1, sticky=NSEW)

Label(app, text='Enter the estminated time being spent on project').grid(row=6, column=1, sticky=NSEW)
Entry(app, textvariable=length).grid(row=7, column=1, sticky=NSEW)

Label(app, text='Enter amount employees will be paided per hour').grid(row=8, column=1, sticky=NSEW)
Entry(app, textvariable=pay).grid(row=9, column=1, sticky=NSEW)

Button(app, text='Calulate', command=math).grid(row=10, column=1, sticky=NSEW)
Button(app, text='Save', command=saving).grid(row=11, column=1, sticky=NSEW)



def display(woodPrice, smallMaterials, totalMaterialCost, totalEmployeePay, Price,GST, Overhead,QuotePrice):
    Label(app, text=woodPrice, relief="sunken").grid(row=12, column=1, sticky=NSEW)
    Label(app, text=smallMaterials, relief="sunken").grid(row=13, column=1, sticky=NSEW)
    Label(app, text=totalMaterialCost, relief="sunken").grid(row=14, column=1, sticky=NSEW)
    Label(app, text=totalEmployeePay, relief="sunken").grid(row=15, column=1, sticky=NSEW)
    Label(app, text=Price, relief="sunken").grid(row=16, column=1, sticky=NSEW)
    Label(app, text=GST, relief="sunken").grid(row=17, column=1, sticky=NSEW)
    Label(app, text=Overhead, relief="sunken").grid(row=18, column=1, sticky=NSEW)
    Label(app, text=QuotePrice, relief="sunken").grid(row=19, column=1, sticky=NSEW)

mainloop()

1 个答案:

答案 0 :(得分:0)

您可以修改分配给每个标签text的字符串。

woodPriceText = "Wood Price: " + str(woodPrice)
Label(app, text=woodPriceText, relief="sunken").grid(row=12, column=1, sticky=NSEW)

或者不要使用临时变量并直接分配,

Label(app, text="Wood Price: " + str(woodPrice), relief="sunken").grid(row=12, column=1, sticky=NSEW)