鱼C论坛

 找回密码
 立即注册
查看: 1042|回复: 8

[已解决]Radiobutton的替换虽然替换了,但是没有完全替换

[复制链接]
发表于 2020-4-1 21:12:16 | 显示全部楼层 |阅读模式

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

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

x
answer[0]有5个值,answer[1]有6个值,answer[1]只显示了5个,没有完全显示出来,怎么办

  1. from tkinter import *

  2. x = 0

  3. root = Tk()

  4. answers = {0: [('草莓', 2), ('苹果', 3), ('西瓜', 5), ('菠萝', 10), ('橘子', 15)],
  5.            1: [('郊外', 2), ('电影院', 3), ('公园', 5), ('商场', 10), ('酒吧', 15), ('练歌房', 20)]}

  6. v = StringVar()
  7. choices = []    # 保存 Radiobutton 的一个列表


  8. for ans, point in answers[x]:
  9.     choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
  10.     choices1.pack()
  11.     choices.append(choices1)


  12. def point_plus_change():
  13.     global x
  14.     x += 1
  15.     x %= 2    # 对 2 求余数
  16.     if x <= 14:
  17.         if x == 0:
  18.             for (ans, point), choices1 in zip(answers[0], choices):
  19.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')
  20.         if x == 1:
  21.             for (ans, point), choices1 in zip(answers[1], choices):
  22.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')


  23. butt_addpoint = Button(root, text='确定', font=('黑体', 10), command=point_plus_change)
  24. butt_addpoint.pack()


  25. root.mainloop()
复制代码
最佳答案
2020-4-1 21:13:38
貌似只能这样……

  1. from tkinter import *

  2. x = 0

  3. root = Tk()

  4. answers = {0: [('草莓', 2), ('苹果', 3), ('西瓜', 5), ('菠萝', 10), ('橘子', 15), ('', 20)],
  5.            1: [('郊外', 2), ('电影院', 3), ('公园', 5), ('商场', 10), ('酒吧', 15), ('练歌房', 20)]}

  6. v = StringVar()
  7. choices = []    # 保存 Radiobutton 的一个列表


  8. for ans, point in answers[x]:
  9.     choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
  10.     choices1.pack()
  11.     choices.append(choices1)


  12. def point_plus_change():
  13.     global x
  14.     x += 1
  15.     x %= 2    # 对 2 求余数
  16.     if x <= 14:
  17.         if x == 0:
  18.             for (ans, point), choices1 in zip(answers[0], choices):
  19.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')
  20.         if x == 1:
  21.             for (ans, point), choices1 in zip(answers[1], choices):
  22.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')


  23. butt_addpoint = Button(root, text='确定', font=('黑体', 10), command=point_plus_change)
  24. butt_addpoint.pack()


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

使用道具 举报

发表于 2020-4-1 21:13:38 | 显示全部楼层    本楼为最佳答案   
貌似只能这样……

  1. from tkinter import *

  2. x = 0

  3. root = Tk()

  4. answers = {0: [('草莓', 2), ('苹果', 3), ('西瓜', 5), ('菠萝', 10), ('橘子', 15), ('', 20)],
  5.            1: [('郊外', 2), ('电影院', 3), ('公园', 5), ('商场', 10), ('酒吧', 15), ('练歌房', 20)]}

  6. v = StringVar()
  7. choices = []    # 保存 Radiobutton 的一个列表


  8. for ans, point in answers[x]:
  9.     choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
  10.     choices1.pack()
  11.     choices.append(choices1)


  12. def point_plus_change():
  13.     global x
  14.     x += 1
  15.     x %= 2    # 对 2 求余数
  16.     if x <= 14:
  17.         if x == 0:
  18.             for (ans, point), choices1 in zip(answers[0], choices):
  19.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')
  20.         if x == 1:
  21.             for (ans, point), choices1 in zip(answers[1], choices):
  22.                 choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')


  23. butt_addpoint = Button(root, text='确定', font=('黑体', 10), command=point_plus_change)
  24. butt_addpoint.pack()


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

使用道具 举报

 楼主| 发表于 2020-4-1 21:19:37 | 显示全部楼层
zltzlt 发表于 2020-4-1 21:13
貌似只能这样……

那如果是有三个值的呢?  
比如再来一个   3: [('草莓', 2), ('苹果', 3), ('西瓜', 5)]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 21:20:21 | 显示全部楼层
你是巨猪怪_ 发表于 2020-4-1 21:19
那如果是有三个值的呢?  
比如再来一个   3: [('草莓', 2), ('苹果', 3), ('西瓜', 5)]

那就在列表后面再加几个 ('', 10)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-1 21:21:41 | 显示全部楼层
zltzlt 发表于 2020-4-1 21:13
貌似只能这样……

我明白了,  但是没有别的办法把多余项删掉吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 21:23:53 | 显示全部楼层
你是巨猪怪_ 发表于 2020-4-1 21:21
我明白了,  但是没有别的办法把多余项删掉吗

删除组件好像可以用组件的 destroy() 方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-1 21:32:49 | 显示全部楼层
zltzlt 发表于 2020-4-1 21:23
删除组件好像可以用组件的 destroy() 方法

如果用destroy()的话不就等于把choices1删除了嘛,那么answer[1]也放不到choices1里了,还是说要建立一个新的组件放answer?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 21:34:01 | 显示全部楼层
你是巨猪怪_ 发表于 2020-4-1 21:32
如果用destroy()的话不就等于把choices1删除了嘛,那么answer[1]也放不到choices1里了,还是说要建立一个 ...

应该是要为 answer[0]、answer[1] 的每一个元素都建立一个 Radiobutton 组件 ,而不只是 config()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-1 21:40:47 | 显示全部楼层
zltzlt 发表于 2020-4-1 21:34
应该是要为 answer[0]、answer[1] 的每一个元素都建立一个 Radiobutton 组件 ,而不只是 config()

请大神指点,我试了还是改不好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 21:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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