python - * : 'instance' and 'float' 不支持的操作数类型

标签 python class tkinter

我是 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/

相关文章:

python - urlopen 是否被延迟评估?

python tkinter - 在按下按钮时重写标签

python - 如何更改 tkFileDialog.asksaveasfilename 中的 Logo ?

python - “wait_window”方法有什么作用?

python - 在稀疏矩阵中排序

python - 在 python 中使用 skpy 在 Skype 中发送消息

python - 如何将 Pandas 系列转换为索引和值的元组

javascript - react `context` 始终为空对象

C++类编译

java - Spring中的路径属性