【问题标题】:How to get an entry from a user?如何从用户那里获取条目?
【发布时间】:2013-08-25 10:03:42
【问题描述】:

这是我的全部代码:

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 () 并且探针开始没有出现。那是因为 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

【问题讨论】:

  • 但我不想将其转换为 str()?

标签: python user-interface tkinter


【解决方案1】:

使用mEntry.get()获取用户提供的文字。所以,bowlingspeedcalc 的第一行应该是:

entry= float(mEntry.get())

另外,我不确定你是如何让mEntrybowlingspeedcalc 可见的。它可能是您帖子的格式。或者您可能需要global

【讨论】:

  • @ShahidIqbal 添加了更多细节。希望它更清楚。请注意,我正在回答您的具体问题“有没有办法可以将 mEntry 转换为用户输入的浮点值?”。如果问题不同,或者您遇到了新问题,请发布新问题并具体说明。
  • 哦,我尝试了不同的方法,它起作用了,您是否在 myif elif 语句中看到我的标签中的错误,因为它现在显示我的标签中的错误:((((
  • @ShahidIqbal 很高兴听到这个消息,Shahid。
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多