我是 Tkinter 的新手。我已经尝试为我正在创建的 GUI 创建一个类,但遇到了一个我无法修复的错误。我浏览过其他论坛并尝试了不同的方法来修正错误,但没有成功。 代码:
root1 = Tk()
root1.geometry("800x500")
class simulation:
def __init__(self, root1):
radius_input = Label ( root1 , text="Radius: " )
direction_input = Label ( root1 , text="Direction (clockwise=1/\n anticlockwise=2): " )
radius_entry = Entry ( root1 )
direction_entry = Entry ( root1 )
#####other code used to display labels and entry boxes
self.radius = radius_entry
self.direction = direction_entry
self.pos_neg = 0
if self.direction == 1:
self.pos_neg = (-6.28)
elif self.direction == 2:
self.pos_neg = (6.28)
def actual_sim(self):
r = self.radius #radius of the circle the ball moves
t = 1
omega = self.pos_neg / t # amount/angle the ball moves each second
#####other code not needed
xposition = (r * cosValue)
yposition = (r * sinValue)
我得到的错误是:
xposition = (r * cosValue)
TypeError: unsupported operand type(s) for *: 'instance' and 'float'
我不明白这意味着什么或如何解决它。有什么想法吗??
最佳答案
错误消息告诉您您正试图将一个实例(即:一个基于某个类的对象)乘以一个 float 。在这种情况下,您乘以一个小部件(不是小部件的内容),这是没有意义的。
您必须修改 actual_sim
以获取值并将其转换为数字,然后才能使用它执行计算:
def actual_sim(self):
r = float(self.radius.get())
...
关于python - * : 'instance' and 'float' 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496636/