2015-08-13 19 views
0

我的代碼是爲了獲取用戶輸入的金額修復邏輯錯誤,然後乘以在下拉菜單中進行了選擇的價值的字典選項。但是,它會將轉換率打印爲用戶輸入的次數。你能幫我在我的代碼在那裏打印的值的時間,而不是產品

from tkinter import * #Imports everything from the tkinter module 
#Imports everything from the math module 
wall = Tk() #Creates the window 
wall.title('CC V2.1.1.2') #Labels the window with the version number 
var = StringVar(wall) 
global ConR 
ConR = 0 
options = { 
    'AU': '1', 
    'US': '.77', 
    'EU': '.55', 
    'Yen': '.011', 
    'NZ': '1.25', 
} 
option = OptionMenu(wall, var, *options)#Conversion Rate, default is one (the value that you entered) 
var.set('AU') 
option.pack() 

def evaluate(event): 
    ConR = options[var.get()] 
    print (ConR) 
    res.configure(text = "Converted value: $" + (str(eval(entry.get()*ConR))))#Takes the value entered from entry.bind and multiplies it by the currency chosen by the user, and prints it at the bottom of the window 
entry = Entry(wall) #Creates the entry widgit 
entry.bind("<Return>", evaluate) 
entry.pack() 
res = Label(wall) 
res.pack() 
y = Button(text='Quit', command=quit)#An exit button 
y.pack() 
wall.mainloop() 

回答

0

很少有什麼問題,我可以看到 -

  1. 首先要定義的轉換率字符串,在你的字典選項,它給一個錯誤我。您應該將浮動值存儲爲值。

  2. 其次,entry.get()即使結果是一個字符串,你應該轉換爲浮動。

  3. 你不應該使用eval(),轉換結果entry.get()浮動和使用float的選項後,可以直接將它們相乘,並顯示結果。

碼 -

from tkinter import * #Imports everything from the tkinter module 
#Imports everything from the math module 
wall = Tk() #Creates the window 
wall.title('CC V2.1.1.2') #Labels the window with the version number 
var = StringVar(wall) 
global ConR 
ConR = 0 
options = { 
    'AU': 1, 
    'US': .77, 
    'EU': .55, 
    'Yen': .011, 
    'NZ': 1.25, 
} 
option = OptionMenu(wall, var, *options)#Conversion Rate, default is one (the value that you entered) 
var.set('AU') 
option.pack() 

def evaluate(event): 
    ConR = options[var.get()] 
    print(repr(ConR)) 
    try: 
     result = float(entry.get()) * ConR 
     res.configure(text = "Converted value: $" + str(result)) 
     #Takes the value entered from entry.bind and multiplies it by the currency chosen by the user, and prints 
    except: 
     res.configure(text = "String not allowed !") 

it at the bottom of the window 
entry = Entry(wall) #Creates the entry widgit 
entry.bind("<Return>", evaluate) 
entry.pack() 
res = Label(wall) 
res.pack() 
y = Button(text='Quit', command=quit)#An exit button 
y.pack() 
wall.mainloop() 
+0

太謝謝你了!如果你有一個reddit賬戶,你可以把它鏈接到我,我可以給你黃金嗎?你不知道我有多愛你。 –

+0

其優良的我只是很高興在這裏代表點:),我想諮詢您接受通過點擊答案左側的刻度標記的答案,這將是有幫助的社區。我很高興我可以幫助:)。 –

相關問題