我需要创建一个gui提示行,允许用户输入两个我可以减去的浮点数

时间:2016-12-14 03:42:44

标签: python user-interface tkinter tkinter-entry

我需要创建一个python gui提示行,允许用户输入两个我可以减去的浮点数。 Entry函数创建一个字符串,因此我不能减去这些数字。我试图围绕创建输入行包装float()函数,但似乎不允许这样做。请帮我创建两个输入框,让用户给出一个项目的成本(4.31美元)和现金(前5.00)我可以减去以找到我需要更改的金额!

import tkinter as tk
from tkinter import *
import subprocess as sub
from tkinter import Tk, Label, Button, Entry, StringVar, DISABLED, NORMAL, END, W, E

master = Tk()

cost_label = Label(master, text="Enter Amount of Cost: $").grid(row=0)
cash_label = Label(master, text="Enter Amount of Cash: $").grid(row=1)
#WHERE I NEED HELP
cost = Entry(master)
cash = Entry(master)

cost.grid(row=0, column=1)
cash.grid(row=1, column=1)


class Change():
    def __init__(self):
        #ignore emptiness

    def changeProgram():
  #while statements to find amount depending on different dollar or coin amount
       #where im using the entries
 amount = cash - cash
        one_hundred_dollars = 0
        while amount >= 100:
            amount -= 100
            one_hundred_dollars += 1 

        fifty_dollars = 0
        while amount >= 50:
            amount -= 50
            fifty_dollars += 1 

        twenty_dollars = 0
        while amount >= 20:
            amount -= 20
            twenty_dollars += 1 

        ten_dollars = 0
        while amount >= 10:
            amount -= 10
            ten_dollars += 1 

        five_dollars = 0
        while amount >= 5:
            amount -= 5
            five_dollars += 1 

        dollars = 0
        while amount >= 1:
            amount -= 1
            dollars += 1 

# find remaining amount of change
        quarters = 0
        while amount >= .25:
            amount -= .25
            quarters += 1

        dimes = 0
        while amount >= .1:
            amount -= .1
            dimes += 1

        nickles = 0
        while amount >= .05:
            amount -= .05
            nickles += 1

# get the number of pennies
        pennies = 0
        while amount >= .01:
            pennies += 1
            amount -= .01

            return one_hundred_dollars, fifty_dollars, twenty_dollars, ten_dollars, five_dollars, dollars, quarters, dimes, nickles, pennies



    btnCalculate = Button(master, text="Calculate", fg="blue", command=changeProgram) #Calculate Button
    btnCalculate.grid(padx=100, pady = 15)

    def printoutput(one_hundred_dollars, fifty_dollars, twenty_dollars, ten_dollars, five_dollars, dollars, quarters, dimes, nickles, pennies):

        output = "1 hundred dollars %s" %(one_hundred_dollars)
        output += "\n fifty"

        messagebox.showinfo("results", output)

'''Label(master, text="One Hundred Dollars").grid(row=4)
one_hundred_dollars_output = Entry(master=None, cnf={one_hundred_dollars}, state="readonly")
one_hundred_dollars_output.grid(row=4, column=1)
one_hundred_dollars_output.set(one_hundred_dollars)'''

''' Label(master, text="Fifty").grid(row=4)

    fifty_dollars_output = Entry(master, width=20, state="readonly")
    fifty_dollars_output.grid(row=4, column=1)
    fifty_dollars_output.set(fifty_dollars)

    twenty_dollars_output = Entry(master, width=20, state="readonly")
    twenty_dollars_output.set(twenty_dollars)

    ten_dollars_output = Entry(master, width=20, state="readonly")
    ten_dollars_output.set(ten_dollars)

    five_dollars_output = Entry(master, width=20, state="readonly")
    five_dollars_output.set(five_dollars)

    dollars_output = Entry(master, width=20, state="readonly")
    dollars_output.set(dollars)

    quarters_output = Entry(master, width=20, state="readonly")
    quarters_output.set(quarters)

    dimes_output = Entry(master, width=20, state="readonly")
    dimes_output.set(dimes)

    nickles_output = Entry(master, width=20, state="readonly")
    nickles_output.set(nickles)

    pennies_output = Entry(master, width=20, state="readonly")
    pennies_output.set(pennies)


master.mainloop()'''

1 个答案:

答案 0 :(得分:1)

要从Entry对象获取数据,请调用其.get()方法,如下所示:

    amount = float(cash.get()) - float(cost.get())