鱼C论坛

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

[已解决]我想实现Ratiobutton更新,结果失败,求解

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

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

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

x
代码如下所示,我想实现Radiobutton的替换,为什么这样替换不了呢?

  1. from tkinter import *

  2. x = 0

  3. root = Tk()

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

  7. v = StringVar()

  8. if x <= 14:
  9.     if x == 0:
  10.         for ans, point in answers[x]:
  11.             choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
  12.             choices1.pack()
  13.     if x == 1:
  14.         for ans, point in answers[1]:
  15.             choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
  16.             choices1.pack()


  17. def point_plus_change():
  18.     global x
  19.     x += 1


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


  22. root.mainloop()
复制代码



点击确认按钮之后没有反应,如图所示

                               
登录/注册后可看大图

最佳答案
2020-4-1 20:13:24
  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()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-1 20:13:24 | 显示全部楼层    本楼为最佳答案   
  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()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-1 20:19:22 | 显示全部楼层


大哥你是真的强  什么时候我也能像你一样就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-1 20:50:59 | 显示全部楼层

大哥,answer[0]有5个值,answer[1]有6个值,answer[1]只显示了5个,没有完全显示出来,怎么办
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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