2015-12-04 183 views
0

這是針對Python 3.4版本的。我在下面的代碼中有什麼作品;但是我不能讓我的DoubleVar()公式攜帶2個十進制格式。我如何將默認值從0.0設置爲0.00,因爲我將此用於貨幣。Tkinter格式化爲小數點後兩位

import tkinter 
from tkinter import ttk 

root = tkinter.Tk() 
root.title("Change Counter") 

ttk.Separator(root).grid(row=3, columnspan=5, sticky="ew") 

dollarCoins = tkinter.IntVar() 
halfDollars = tkinter.IntVar() 
quarters = tkinter.IntVar() 
dimes = tkinter.IntVar() 
nickels = tkinter.IntVar() 
pennies = tkinter.IntVar() 

dollarCoinsTotal = tkinter.DoubleVar() 
halfDollarTotal = tkinter.DoubleVar() 
quartersTotal = tkinter.DoubleVar() 
dimesTotal = tkinter.DoubleVar() 
nickelsTotal = tkinter.DoubleVar() 
penniesTotal = tkinter.DoubleVar() 

titleLabel = tkinter.Label(root, text = "Change Counter") 
titleLabel.grid(row=1, column=1, columnspan = 4) 

def Totals(): 
    dollarCoinsTotal.set (float(dollarCoins.get())) 
    halfDollarTotal.set(float(halfDollars.get()* 0.50)) 
    quartersTotal.set(float(quarters.get()* 0.25)) 
    dimesTotal.set(float(dimes.get()* 0.10)) 
    nickelsTotal.set(float(nickels.get()* 0.05)) 
    penniesTotal.set(float(pennies.get()* 0.01)) 

titleLabel = tkinter.Label(root, text = "Enter the number of each coin type and hit compute") 
titleLabel.grid(row=2, column=1, columnspan =4) 

dollarCoinsLabel = tkinter.Label(root, text = "Dollar Coins") 
dollarCoinsLabel.grid(row=4, column=1, pady= 5) 

dollarCoinsEntry = tkinter.Entry(root, textvariable = dollarCoins) 
dollarCoinsEntry.grid(row=4, column=2, pady= 5, padx = 10) 

halfDollarsLabel = tkinter.Label(root, text = "Half Dollars") 
halfDollarsLabel.grid(row=5, column=1, pady= 5, padx = 10) 

halfDollarsEntry = tkinter.Entry(root, textvariable = halfDollars) 
halfDollarsEntry.grid(row=5, column=2, pady= 5) 

quartersLabel = tkinter.Label(root, text = "Quarters") 
quartersLabel.grid(row=6, column=1, pady= 5, padx = 10) 

quartersEntry = tkinter.Entry(root, textvariable = quarters) 
quartersEntry.grid(row=6, column=2, pady= 5) 

dimesLabel = tkinter.Label(root, text = "Dimes") 
dimesLabel.grid(row=7, column=1, pady= 5, padx = 10) 

dimesEntry = tkinter.Entry(root, textvariable = dimes) 
dimesEntry.grid(row=7, column=2, pady= 5) 

nickelsLabel = tkinter.Label(root, text = "Nickels") 
nickelsLabel.grid(row=8, column=1, pady= 5) 

nickelsEntry = tkinter.Entry(root, textvariable = nickels) 
nickelsEntry.grid(row=8, column=2, pady= 5, padx = 10) 

penniesLabel = tkinter.Label(root, text = "Pennies") 
penniesLabel.grid(row=9, column=1, pady= 5, padx = 10) 

penniesEntry = tkinter.Entry(root, textvariable = pennies) 
penniesEntry.grid(row=9, column=2, pady= 5) 

dollarCoinsLabelText = tkinter.Label(root, text = "Dollar Coins Value: $") 
dollarCoinsLabelText.grid(row=4, column=3, pady= 5, padx = 10) 

dollarCoinsLabelValue = tkinter.Label(root, textvariable = dollarCoinsTotal) 
dollarCoinsLabelValue.grid(row=4, column=4, pady= 5) 

halfDollarLabelText = tkinter.Label(root, text = "Half Dollar Value: $") 
halfDollarLabelText.grid(row=5, column=3, pady= 5, padx = 10) 

halfDollarLabelValue = tkinter.Label(root, textvariable = halfDollarTotal) 
halfDollarLabelValue.grid(row=5, column=4, pady= 5) 

quartersLabelText = tkinter.Label(root, text = "Quarters Value: $") 
quartersLabelText.grid(row=6, column=3, pady= 5, padx = 10) 

quartersLabelValue = tkinter.Label(root, textvariable = quartersTotal) 
quartersLabelValue.grid(row=6, column=4, pady= 5) 

dimesLabelText = tkinter.Label(root, text = "Dimes Value: $") 
dimesLabelText.grid(row=7, column=3, pady= 5, padx = 10) 

dimesLabelValue = tkinter.Label(root, textvariable = dimesTotal) 
dimesLabelValue.grid(row=7, column=4, pady= 5) 

nickelsLabelText = tkinter.Label(root, text = "Nickels Value: $") 
nickelsLabelText.grid(row=8, column=3, pady= 5, padx = 10) 

nickelsLabelValue = tkinter.Label(root, textvariable = nickelsTotal) 
nickelsLabelValue.grid(row=8, column=4, pady= 5) 

penniesLabelText = tkinter.Label(root, text = "Pennies Value: $") 
penniesLabelText.grid(row=9, column=3, pady= 5, padx = 10) 

penniesLabelValue = tkinter.Label(root, textvariable = penniesTotal) 
penniesLabelValue.grid(row=9, column=4, pady= 5) 

calculateButton = tkinter.Button(root, text = "Compute", command = Totals) 
calculateButton.grid(row=10, column=1) 


root.mainloop() 
+0

我不知道這是否是蟒蛇有關,但一個整數是一個整數,而浮點數是一個小數數。 – Inkblot

+0

墨跡你是正確的。這是一個使用Tkinter的GUI,所以我以Int格式取值並將它們轉換爲浮點數。該程序旨在採取一定數量的硬幣,並給你加上一起硬幣的實際價值 – jamesicywhite

回答

0

您可以使用.format(),但是您的變量將被轉換爲字符串

dollars = 99.9       #<== float value 
strDollars = '{:.2f}'.format(dollars) 
# strDollars = '99.90'     <== string value 

我希望這有助於!

+0

我需要它作爲一個DoubleVar是我正在尋找的,所以我可以更新輸入,它將重新評估點擊按鈕。 – jamesicywhite

0

不要使用二進制浮點計算貨幣,因爲小數值不能完全轉換。例如,float(.1)並不是真正的.1。要麼使用整數便士,格式化爲字符串作爲美元,或者使用decimal.Decimals。我做了前者。以下唯一更改是從DoubleVar切換到StringVar,新的form()函數和總計版本。

import tkinter 
from tkinter import ttk 

root = tkinter.Tk() 
root.title("Change Counter") 

ttk.Separator(root).grid(row=3, columnspan=5, sticky="ew") 

dollarCoins = tkinter.IntVar() 
halfDollars = tkinter.IntVar() 
quarters = tkinter.IntVar() 
dimes = tkinter.IntVar() 
nickels = tkinter.IntVar() 
pennies = tkinter.IntVar() 

dollarCoinsTotal = tkinter.StringVar() 
halfDollarTotal = tkinter.StringVar() 
quartersTotal = tkinter.StringVar() 
dimesTotal = tkinter.StringVar() 
nickelsTotal = tkinter.StringVar() 
penniesTotal = tkinter.StringVar() 

titleLabel = tkinter.Label(root, text = "Change Counter") 
titleLabel.grid(row=1, column=1, columnspan = 4) 

def form(pennies): 
    return '{:d}.{:02d}'.format(*divmod(pennies, 100)) 

def Totals(): 
    dollarCoinsTotal.set (form(dollarCoins.get() * 100)) 
    halfDollarTotal.set(form(halfDollars.get() * 50)) 
    quartersTotal.set(form(quarters.get() * 25)) 
    dimesTotal.set(form(dimes.get() * 10)) 
    nickelsTotal.set(form(nickels.get() * 5)) 
    penniesTotal.set(form(pennies.get() * 1)) 

titleLabel = tkinter.Label(root, text = "Enter the number of each coin type and hit compute") 
titleLabel.grid(row=2, column=1, columnspan =4) 

dollarCoinsLabel = tkinter.Label(root, text = "Dollar Coins") 
dollarCoinsLabel.grid(row=4, column=1, pady= 5) 

dollarCoinsEntry = tkinter.Entry(root, textvariable = dollarCoins) 
dollarCoinsEntry.grid(row=4, column=2, pady= 5, padx = 10) 

halfDollarsLabel = tkinter.Label(root, text = "Half Dollars") 
halfDollarsLabel.grid(row=5, column=1, pady= 5, padx = 10) 

halfDollarsEntry = tkinter.Entry(root, textvariable = halfDollars) 
halfDollarsEntry.grid(row=5, column=2, pady= 5) 

quartersLabel = tkinter.Label(root, text = "Quarters") 
quartersLabel.grid(row=6, column=1, pady= 5, padx = 10) 

quartersEntry = tkinter.Entry(root, textvariable = quarters) 
quartersEntry.grid(row=6, column=2, pady= 5) 

dimesLabel = tkinter.Label(root, text = "Dimes") 
dimesLabel.grid(row=7, column=1, pady= 5, padx = 10) 

dimesEntry = tkinter.Entry(root, textvariable = dimes) 
dimesEntry.grid(row=7, column=2, pady= 5) 

nickelsLabel = tkinter.Label(root, text = "Nickels") 
nickelsLabel.grid(row=8, column=1, pady= 5) 

nickelsEntry = tkinter.Entry(root, textvariable = nickels) 
nickelsEntry.grid(row=8, column=2, pady= 5, padx = 10) 

penniesLabel = tkinter.Label(root, text = "Pennies") 
penniesLabel.grid(row=9, column=1, pady= 5, padx = 10) 

penniesEntry = tkinter.Entry(root, textvariable = pennies) 
penniesEntry.grid(row=9, column=2, pady= 5) 

dollarCoinsLabelText = tkinter.Label(root, text = "Dollar Coins Value: $") 
dollarCoinsLabelText.grid(row=4, column=3, pady= 5, padx = 10) 

dollarCoinsLabelValue = tkinter.Label(root, textvariable = dollarCoinsTotal) 
dollarCoinsLabelValue.grid(row=4, column=4, pady= 5) 

halfDollarLabelText = tkinter.Label(root, text = "Half Dollar Value: $") 
halfDollarLabelText.grid(row=5, column=3, pady= 5, padx = 10) 

halfDollarLabelValue = tkinter.Label(root, textvariable = halfDollarTotal) 
halfDollarLabelValue.grid(row=5, column=4, pady= 5) 

quartersLabelText = tkinter.Label(root, text = "Quarters Value: $") 
quartersLabelText.grid(row=6, column=3, pady= 5, padx = 10) 

quartersLabelValue = tkinter.Label(root, textvariable = quartersTotal) 
quartersLabelValue.grid(row=6, column=4, pady= 5) 

dimesLabelText = tkinter.Label(root, text = "Dimes Value: $") 
dimesLabelText.grid(row=7, column=3, pady= 5, padx = 10) 

dimesLabelValue = tkinter.Label(root, textvariable = dimesTotal) 
dimesLabelValue.grid(row=7, column=4, pady= 5) 

nickelsLabelText = tkinter.Label(root, text = "Nickels Value: $") 
nickelsLabelText.grid(row=8, column=3, pady= 5, padx = 10) 

nickelsLabelValue = tkinter.Label(root, textvariable = nickelsTotal) 
nickelsLabelValue.grid(row=8, column=4, pady= 5) 

penniesLabelText = tkinter.Label(root, text = "Pennies Value: $") 
penniesLabelText.grid(row=9, column=3, pady= 5, padx = 10) 

penniesLabelValue = tkinter.Label(root, textvariable = penniesTotal) 
penniesLabelValue.grid(row=9, column=4, pady= 5) 

calculateButton = tkinter.Button(root, text = "Compute", command = Totals) 
calculateButton.grid(row=10, column=1) 

root.mainloop() 

過度的重複會爲某種重構而尖叫,但我把它作爲一個練習。

+0

這是我遇到的解決方案: – jamesicywhite

0

這是我來到該解決方案爲我試圖做的事:

import tkinter 
from tkinter import ttk 

root = tkinter.Tk() 
root.title("Change Counter") 

#separator line 
ttk.Separator(root).grid(row=2, columnspan=5, sticky="ew") 

#Input Integers 
dollarCoins = tkinter.IntVar() 
halfDollars = tkinter.IntVar() 
quarters = tkinter.IntVar() 
dimes = tkinter.IntVar() 
nickels = tkinter.IntVar() 
pennies = tkinter.IntVar() 

#Output Floats on right Side 
dollarCoinsTotal = tkinter.DoubleVar() 
dollarCoinsTotal.set('0.00') 
halfDollarTotal = tkinter.DoubleVar() 
halfDollarTotal.set('0.00') 
quartersTotal = tkinter.DoubleVar() 
quartersTotal.set('0.00') 
dimesTotal = tkinter.DoubleVar() 
dimesTotal.set('0.00') 
nickelsTotal = tkinter.DoubleVar() 
nickelsTotal.set('0.00') 
penniesTotal = tkinter.DoubleVar() 
penniesTotal.set('0.00') 
allTotal = tkinter.DoubleVar() 
allTotal.set('0.00') 

#Title Document 
titleLabel = tkinter.Label(root, text = "Change Counter", font = "-weight bold") 
titleLabel.grid(row=1, column=1, columnspan = 4) 

#Define Function for Button 
#Format to be a floating point number of 2 decimals 
def Totals(): 
    dollarCoinsTotal.set("{:.2f}".format(float(dollarCoins.get()))) 
    halfDollarTotal.set("{:.2f}".format(float(halfDollars.get()* 0.50))) 
    quartersTotal.set("{:.2f}".format(float(quarters.get()* 0.25))) 
    dimesTotal.set("{:.2f}".format(float(dimes.get()* 0.10))) 
    nickelsTotal.set("{:.2f}".format(float(nickels.get()* 0.05))) 
    penniesTotal.set("{:.2f}".format(float(pennies.get()* 0.01))) 
    allTotal.set("{:.2f}".format(float(dollarCoinsTotal.get() + halfDollarTotal.get() + quartersTotal.get() + dimesTotal.get() + nickelsTotal.get() + penniesTotal.get()))) 

#Input statment for user 
titleLabel = tkinter.Label(root, text = "Enter the number of each coin type and hit compute", font="-size 11") 
titleLabel.grid(row=3, column=1, columnspan =4) 

#Input Labels for Integers 
dollarCoinsLabel = tkinter.Label(root, text = "Dollar Coins") 
dollarCoinsLabel.grid(row=4, column=1, pady= 5) 

dollarCoinsEntry = tkinter.Entry(root, textvariable = dollarCoins) 
dollarCoinsEntry.grid(row=4, column=2, pady= 5, padx = 10) 

halfDollarsLabel = tkinter.Label(root, text = "Half Dollars") 
halfDollarsLabel.grid(row=5, column=1, pady= 5, padx = 10) 

halfDollarsEntry = tkinter.Entry(root, textvariable = halfDollars) 
halfDollarsEntry.grid(row=5, column=2, pady= 5) 

quartersLabel = tkinter.Label(root, text = "Quarters") 
quartersLabel.grid(row=6, column=1, pady= 5, padx = 10) 

quartersEntry = tkinter.Entry(root, textvariable = quarters) 
quartersEntry.grid(row=6, column=2, pady= 5) 

dimesLabel = tkinter.Label(root, text = "Dimes") 
dimesLabel.grid(row=7, column=1, pady= 5, padx = 10) 

dimesEntry = tkinter.Entry(root, textvariable = dimes) 
dimesEntry.grid(row=7, column=2, pady= 5) 

nickelsLabel = tkinter.Label(root, text = "Nickels") 
nickelsLabel.grid(row=8, column=1, pady= 5) 

nickelsEntry = tkinter.Entry(root, textvariable = nickels) 
nickelsEntry.grid(row=8, column=2, pady= 5, padx = 10) 

penniesLabel = tkinter.Label(root, text = "Pennies") 
penniesLabel.grid(row=9, column=1, pady= 5, padx = 10) 

penniesEntry = tkinter.Entry(root, textvariable = pennies) 
penniesEntry.grid(row=9, column=2, pady= 5) 

#Output Labels for DoubleVars 
dollarCoinsLabelText = tkinter.Label(root, text = "Dollar Coins Value: $") 
dollarCoinsLabelText.grid(row=4, column=3, pady= 5, padx = 10) 

dollarCoinsLabelValue = tkinter.Label(root, textvariable = dollarCoinsTotal) 
dollarCoinsLabelValue.grid(row=4, column=4, pady= 5) 

halfDollarLabelText = tkinter.Label(root, text = "Half Dollar Value: $") 
halfDollarLabelText.grid(row=5, column=3, pady= 5, padx = 10) 

halfDollarLabelValue = tkinter.Label(root, textvariable = halfDollarTotal) 
halfDollarLabelValue.grid(row=5, column=4, pady= 5) 

quartersLabelText = tkinter.Label(root, text = "Quarters Value: $") 
quartersLabelText.grid(row=6, column=3, pady= 5, padx = 10) 

quartersLabelValue = tkinter.Label(root, textvariable = quartersTotal) 
quartersLabelValue.grid(row=6, column=4, pady= 5) 

dimesLabelText = tkinter.Label(root, text = "Dimes Value: $") 
dimesLabelText.grid(row=7, column=3, pady= 5, padx = 10) 

dimesLabelValue = tkinter.Label(root, textvariable = dimesTotal) 
dimesLabelValue.grid(row=7, column=4, pady= 5) 

nickelsLabelText = tkinter.Label(root, text = "Nickels Value: $") 
nickelsLabelText.grid(row=8, column=3, pady= 5, padx = 10) 

nickelsLabelValue = tkinter.Label(root, textvariable = nickelsTotal) 
nickelsLabelValue.grid(row=8, column=4, pady= 5) 

penniesLabelText = tkinter.Label(root, text = "Pennies Value: $") 
penniesLabelText.grid(row=9, column=3, pady= 5, padx = 10) 

penniesLabelValue = tkinter.Label(root, textvariable = penniesTotal) 
penniesLabelValue.grid(row=9, column=4, pady= 5) 

allTotalLabelText = tkinter.Label(root, text = "Total Change Value: $", font="-weight bold -size 10",) 
allTotalLabelText.grid(row=10, column=3, pady= 5, padx = 10) 

allTotalLabelValue = tkinter.Label(root, textvariable = allTotal) 
allTotalLabelValue.grid(row=10, column=4, pady= 5) 

#Button to run calculations 
calculateButton = tkinter.Button(root, text = "Compute", relief="raised", fg="red", font="-weight bold", command = Totals) 
calculateButton.grid(row=10, column=1, columnspan =2, sticky = "EW", padx=5) 


root.mainloop() 
相關問題