2017-04-08 60 views
0
from tkinter import * 


pencere = Tk() #Pencereyi Oluşturyor 

def Ekmek(event): 
    print(0.6*x)   
#Here i am trying to calcute the carbohydrate for diabetes 
#1 gram of bread is 0.6 carbohydrate and i am trying to calculate the gram user enters 
def Patates(event): 
    print(0.16666666666666666) 

gramy = Label(text = "Yemeğin Gramını Girin:",fg = "green",bg = "black") 
gram = Entry(fg = "green",bg = "black") 
x = gram.get() 

#In this part i want to change the "x" to a 
#float value but it gives error.How can i fix it ? 


ekmek = Button(text="Ekmek",fg="orange",bg = "black") 
ekmek.bind("<Button-1>",Ekmek) 
patates = Button(text="Patates",fg="orange",bg = "black") 
patates.bind("<Button-1>",Patates) 
makarna = Button(text="Makarna",fg="orange",bg = "black") 
pilav = Button(text="Pilav",fg="orange",bg = "black") 

gramy.grid(row=0,column=1) 
gram.grid(row =0,column =2) 
ekmek.grid(row =1,column = 0) 
patates.grid(row =1,column = 1) 
makarna.grid(row = 1,column = 2) 
pilav.grid(row = 2,column = 0) 



pencere.mainloop() #Pencerenin Çarpıya 
+0

不要只是說它給錯誤,什麼錯誤?首先,我建議你重新學習tkinter,因爲你永遠不會讓用戶寫入任何東西。綁定到一個按鈕或什麼 – abccd

+0

但用戶打算進入克,所以如果用戶說230克,並說我想要麪包它將要做(0.6 * 230),所以用戶可以選擇幾乎所有的東西 – dartyet1

回答

0

你的問題的代碼是非常有問題的,因爲「X」將永遠是""(空字符串),因爲你把它直接在其創作不給任何時候用戶輸入。如你所知,""不能被浮動。

這是你的問題代碼:

gram = Entry(fg = "green",bg = "black") 
x = gram.get() 

你應該把這裏gram.get:

def Ekmek(event): 
    x = gram.get() 
    x=float(x.split('gram')[0]) 
    print(0.6*x) 

用戶按下按鈕後,這將檢索X。

另一種方法是,你可以使用一個StringVar。

+1

爲什麼使用' StringVar更好?它增加了一個必須管理的額外對象,但不提供任何額外的值。如果你有理由認爲它更好,如果你解釋原因,你的答案可以得到改善。 –