Python 在第二页上不显示小部件?
这是我的全部代码:Python 在第二页上不显示小部件?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,这是我的全部代码: import sys import tkinter as tk import os def bowlingspeedcalc (): if type(mEntry) == float: speed = 0.01890*3600/mEntry if speed >165: tk.Label (str(speed) + " kph !!! You don't bowl that fast!!!o_0 ").
import sys
import tkinter as tk
import os
def bowlingspeedcalc ():
if type(mEntry) == float:
speed = 0.01890*3600/mEntry
if speed >165:
tk.Label (str(speed) + " kph !!! You don't bowl that fast!!!o_0 ").place (x = 50,y = 50)
elif speed >=140:
tk.Label ("You are a fast bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50)
elif speed >= 130:
tk.Label ("You are a fast-medium bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50)
elif speed >= 120:
tk.Label ("You are a medium-fast bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50)
elif speed >= 105:
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:
tk.Label ("You are a spin bowler,you bowl at: " + str(speed) + " kilometers per hour").place (x = 50,y = 50)
elif speed <= 60:
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]
bowlingspeedcalc ()
for widget in widgets:
widget.place_forget ()
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)
导入系统
将tkinter作为tk导入
导入操作系统
def bowlingspeedcalc():
如果类型(mEntry)=浮动:
速度=0.01890*3600/分钟
如果速度>165:
标签(str(speed)+“kph!!!你打得没那么快!!!o_0”)。位置(x=50,y=50)
elif速度>=140:
标签(“你是一个快速投球手,你的投球速度为:”+str(速度)+“公里/小时”)。地点(x=50,y=50)
elif速度>=130:
标签(“你是一个中等速度的投球手,你的投球速度是:”+str(速度)+“千米每小时”)。地点(x=50,y=50)
elif速度>=120:
标签(“你是一个中速投球手,你的投球速度是:”+str(速度)+“公里/小时”)。地点(x=50,y=50)
elif速度>=105:
标签(“你是一名中速投球手,你的投球速度为:”+str(速度)+“公里/小时”)。位置(x=50,y=50)
elif速度<105且速度>60:
标签(“你是一个旋转投球手,你的投球速度为:”+str(速度)+“公里/小时”)。位置(x=50,y=50)
elif speed您的代码调用bowlingspeedcalc,它试图创建一些小部件。在下一行中,您有一个从屏幕上删除所有小部件的循环
另外,
bowlingspeedcalc
的第一行将mEntry
的类型与float进行比较,float总是会失败mEntry
是一个小部件,而不是一个数字。如果要获取用户输入的数据,必须调用该小部件的get
方法。这将始终返回一个字符串,因此您需要将其转换为一个数字。如果不看到可复制的情况,我们无法猜测代码存在错误的原因。至少,我们需要看看您在哪里定义了mlabel
,mlabel2
,以及创建第二页的代码。谢谢,因为我是python新手,请问在哪里可以找到“get”方法?@ShahidIqbal:可能在与tkinter Entry小部件相关的所有教程和文档中都提到过它。这里有一个:是的,但是get方法是用来获取字符串值的,所以你知道从输入框中检索浮点值的方法吗?@ShahidIqbal:没有浮点值,所以无法获取浮点值。小部件仅支持字符串值。如果你想要一个浮点,你可以用普通的python方法来转换它。你说的普通python方法是什么意思?