歌者文明清理员 发表于 2023-1-31 10:50:03

为什么报错TypeError

源代码
from tkinter import *
from pandas import read_csv

def create(label):
    scale = Scale(
      root, from_=1, to=100, orient=横向, length=550, label=label,
      command=update
    )
    globals() = scale

def update(v):
    global count
    draw('Sun', v)
    count += 1

def draw(planet, v):
    r = radius * v / scale
    canvas.create_oval(0, 0, r, r, fill=color)

横向 = HORIZONTAL
root = Tk(className='Solar system scale')
root.geometry('600x775')
root.resizable(False, False)
canvas = Canvas(root, width=600, height=600, bg='black')
canvas.place(x=0, y=0)
create('大小')
create('距离')
scale大小.place(x=25, y=625)
scale距离.place(x=25, y=700)
count = 0
编号范围 = range(1, 10) # 太阳+水金地火木土天海

data = read_csv('data.csv', index_col=0)
color = data['color']
radius = data['radius']
distance = data['distance']
# 600像素 = 海王星距离
scale = distance['Neptune'] / 600 # 这是1像素代表的距离

update(114)
root.mainloop()

报错信息
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\34459\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
File "C:\Users\34459\Documents\python_programs\solar_system_scale_TKINTER.py", line 13, in update
    draw('Sun', v)
File "C:\Users\34459\Documents\python_programs\solar_system_scale_TKINTER.py", line 17, in draw
    r = radius * v / scale
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
csv
radius,color,distance
Sun,695500,yellow,0
Mercury,2440,gray,57909050
Venus,6051.8,orange,108209184
Earth,6372.8,blue,149597871
Mars,3390,red,227954150
Jupiter,71400,navajowhite,778547200
Saturn,57330,antiquewhite,1433649370
Uranus,25300,cyan,2876674083
Neptune,24553,blue,4503443662

isdkz 发表于 2023-1-31 11:13:45

from tkinter import *
from pandas import read_csv

def create(label):
    scale = Scale(
      root, from_=1, to=100, orient=横向, length=550, label=label,
      command=update
    )
    globals() = scale

def update(v):
    global count
    draw('Sun', int(v))                  # 通过滚动条获取到的 v 为字符串,要用 int 转成整数
    count += 1

def draw(planet, v):
    r = radius * v / scale
    canvas.create_oval(0, 0, r, r, fill=color)

横向 = HORIZONTAL
root = Tk(className='Solar system scale')
root.geometry('600x775')
root.resizable(False, False)
canvas = Canvas(root, width=600, height=600, bg='black')
canvas.place(x=0, y=0)
create('大小')
create('距离')
scale大小.place(x=25, y=625)
scale距离.place(x=25, y=700)
count = 0
编号范围 = range(1, 10) # 太阳+水金地火木土天海

data = read_csv('data.csv', index_col=0)
color = data['color']
radius = data['radius']
distance = data['distance']
# 600像素 = 海王星距离
scale = distance['Neptune'] / 600 # 这是1像素代表的距离

update(114)
root.mainloop()

歌者文明清理员 发表于 2023-1-31 12:54:48

isdkz 发表于 2023-1-31 11:13


谢谢
刚开始还以为是radius为float64的问题,查了一大堆资料没用{:10_257:}

isdkz 发表于 2023-1-31 12:55:49

歌者文明清理员 发表于 2023-1-31 12:54
谢谢
刚开始还以为是radius为float64的问题,查了一大堆资料没用

不客气{:5_109:}
页: [1]
查看完整版本: 为什么报错TypeError