鱼C论坛

 找回密码
 立即注册
查看: 1871|回复: 3

[已解决]为什么报错TypeError

[复制链接]
发表于 2023-1-31 10:50:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
源代码
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()[f'scale{label}'] = scale

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

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

横向 = 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[planet] * 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
最佳答案
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()[f'scale{label}'] = scale

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

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

横向 = 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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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()[f'scale{label}'] = scale

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

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

横向 = 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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-31 12:54:48 | 显示全部楼层

谢谢
刚开始还以为是radius[planet]为float64的问题,查了一大堆资料没用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不客气
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 19:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表