鱼C论坛

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

[已解决]为什么报错TypeError

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

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

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

x
源代码
  1. from tkinter import *
  2. from pandas import read_csv

  3. def create(label):
  4.     scale = Scale(
  5.         root, from_=1, to=100, orient=横向, length=550, label=label,
  6.         command=update
  7.     )
  8.     globals()[f'scale{label}'] = scale

  9. def update(v):
  10.     global count
  11.     draw('Sun', v)
  12.     count += 1

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

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

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

  34. update(114)
  35. root.mainloop()
复制代码

报错信息
  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3.   File "C:\Users\34459\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
  4.     return self.func(*args)
  5.   File "C:\Users\34459\Documents\python_programs\solar_system_scale_TKINTER.py", line 13, in update
  6.     draw('Sun', v)
  7.   File "C:\Users\34459\Documents\python_programs\solar_system_scale_TKINTER.py", line 17, in draw
  8.     r = radius[planet] * v / scale
  9. TypeError: can't multiply sequence by non-int of type 'numpy.float64'
复制代码

csv
  1. radius,color,distance
  2. Sun,695500,yellow,0
  3. Mercury,2440,gray,57909050
  4. Venus,6051.8,orange,108209184
  5. Earth,6372.8,blue,149597871
  6. Mars,3390,red,227954150
  7. Jupiter,71400,navajowhite,778547200
  8. Saturn,57330,antiquewhite,1433649370
  9. Uranus,25300,cyan,2876674083
  10. Neptune,24553,blue,4503443662
复制代码
最佳答案
2023-1-31 11:13:45
  1. from tkinter import *
  2. from pandas import read_csv

  3. def create(label):
  4.     scale = Scale(
  5.         root, from_=1, to=100, orient=横向, length=550, label=label,
  6.         command=update
  7.     )
  8.     globals()[f'scale{label}'] = scale

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

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

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

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

  34. update(114)
  35. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-31 11:13:45 | 显示全部楼层    本楼为最佳答案   
  1. from tkinter import *
  2. from pandas import read_csv

  3. def create(label):
  4.     scale = Scale(
  5.         root, from_=1, to=100, orient=横向, length=550, label=label,
  6.         command=update
  7.     )
  8.     globals()[f'scale{label}'] = scale

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

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

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

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

  34. update(114)
  35. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢
刚开始还以为是radius[planet]为float64的问题,查了一大堆资料没用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

不客气
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 00:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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