我如何在我的python计算器上制作一个平方根按钮(我是一个初学者)


def btnSquareRoot(self):
result = False
current = math.sqrt(text_Input)
text_Input.set(current)

这是我尝试使用的代码,但我不能,因为text_Input是一个字符串var我是一个初学者,这就是为什么对我来说很难做到这一点,但我的想法是以某种方式转换它,但我不知道

将text_input转换为int或float,然后就可以找到平方根。

math.sqrt(int(text_Input)) or math.sqrt(float(text_Input))

要将字符串转换为浮点,您可以按照以下步骤使用浮点函数:

def btnSquareRoot(self): 
result = False 
current = math.sqrt(float(text_Input)) 
text_Input.set(current)

您可以使用str(i)将浮点数字转换为字符串,使用float(x)将字符串转换为浮点数字。所以你的代码应该是text_Input.set(str(current))而不是text_Input.set(current)math.sqrt(float(text_Input.get()))而不是math.sqrt(text_Input)

最新更新