将条目更改为浮动

时间:2017-04-08 14:15:38

标签: python tkinter tkinter-entry

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 

1 个答案:

答案 0 :(得分:0)

您的问题代码存在问题,因为&#39; x&#39;将永远是""(空字符串),因为您直接将其放置在其创建之下,而不给用户输入任何时间。如您所知,""无法浮出水面。

这是您的问题代码:

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。