2013-08-25 41 views
0

這是wholeof我的代碼:如何從用戶獲取條目?

import sys 
import tkinter as tk 
import os 

def bowlingspeedcalc(): 
     entry = float(mEntry) 
     if type(entry) == float: 
      speed = 0.01890*3600/entry 

      if speed >165: 
       Label1 = tk.Label (str(speed) + " kph !!! You don't bowl that fast!!!o_0 ").place (x = 50,y = 50)        

      elif speed >=140: 
       Label2 = tk.Label ("You are a fast bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50) 

      elif speed >= 130: 
       Label3 = tk.Label ("You are a fast-medium bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50) 

      elif speed >= 120: 
       Label4 = tk.Label ("You are a medium-fast bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50) 

      elif speed >= 105: 
       Label5 = tk.Label ("You are a medium pace bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50) 

      elif speed < 105 and speed > 60: 
       Label6 = tk.Label ("You are a spin bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50) 

      elif speed <= 60: 
       Label7 = tk.Label ("You bowl at: " + str(speed) + " kilometers per hour; you bowl like my grandma!!!").place (x = 50,y = 50) 

def forget_page(): 
    widgets = [mlabel1,mlabel2,mlabel3,mEntry,mButton] 
    for widget in widgets: 
     widget.place_forget() 
    bowlingspeedcalc() 


    mGui = tk.Tk() 
    mGui.geometry("300x300") 
    mGui.title("YourBowlingSpeed") 
    mlabel1 = tk.Label (text = "How much time does your ball take to cover the") 
    mlabel2 = tk.Label (text = "20m/22yrds ,from the release to crossing ") 
    mlabel3 = tk.Label (text = "the stumps?") 
    mlabel1.place (x = 20,y = 5) 
    mlabel2.place (x = 17,y = 22) 
    mlabel3.place (x = 130,y = 39) 
    mEntry = tk.Entry() 
    mEntry.place (x = 115,y = 56) 
    mButton = tk.Button (text = "Calculate",command = forget_page) 
    mButton.place (x = 125, y = 73) 
    mGui.mainloop() 

這是我的代碼,我知道是什麼問題,但我不知道如何解決它。一個命令去bowlingspeedcalc()和那裏探頭開始nohing來。這是因爲mEntry不能轉換爲浮點數,但有沒有辦法我可以將mEntry轉換爲用戶輸入的浮點值?任何幫助,我會感激!

這是錯誤消息:

Exception in Tkinter callback 
Traceback (most recent call last): 
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ 
return self.func(*args) 
File "C:\Python33\Projects\YourBowlingSpeed.py", line 36, in forget_page 

bowlingspeedcalc() 
File "C:\Python33\Projects\YourBowlingSpeed.py", line 7, in bowlingspeedcalc 
entry = float(mEntry) 
TypeError: float() argument must be a string or a number 
+0

但我不想把它轉換成一個str()? –

回答

0

使用mEntry.get()以獲得由用戶提供的文本。所以,bowlingspeedcalc第一行應該是:

entry= float(mEntry.get()) 

此外,我不知道你是怎麼做mEntry可見bowlingspeedcalc。這可能是您的帖子的格式。或者你可能需要一個global

+0

我不工作什麼都沒有顯示?! –

+0

@ShahidIqbal增加了一些細節。希望它更清晰。請注意,我正在回答您的具體問題「是否可以將mEntry轉換爲用戶輸入的浮點值?」。如果問題不同,或者您遇到新問題,請發佈新問題並具體說明。 –

+0

哦,我嘗試了不同的方式,它的工作原理你在我的標籤在myif elif語句中看到一個錯誤,因爲它現在顯示我在我的標籤中有錯誤:((((( –