你是巨猪怪_ 发表于 2020-4-1 20:05:28

我想实现Ratiobutton更新,结果失败,求解

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

from tkinter import *

x = 0

root = Tk()

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

v = StringVar()

if x <= 14:
    if x == 0:
      for ans, point in answers:
            choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
            choices1.pack()
    if x == 1:
      for ans, point in answers:
            choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
            choices1.pack()


def point_plus_change():
    global x
    x += 1


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


root.mainloop()


点击确认按钮之后没有反应,如图所示
https://s1.ax1x.com/2020/04/01/G8PwI1.png

zltzlt 发表于 2020-4-1 20:13:24

from tkinter import *

x = 0

root = Tk()

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

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


for ans, point in answers:
    choices1 = Radiobutton(root, text=ans, variable=v, value=point, padx=100, justify='left')
    choices1.pack()
    choices.append(choices1)


def point_plus_change():
    global x
    x += 1
    x %= 2    # 对 2 求余数
    if x <= 14:
      if x == 0:
            for (ans, point), choices1 in zip(answers, choices):
                choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')
      if x == 1:
            for (ans, point), choices1 in zip(answers, choices):
                choices1.config(text=ans, variable=v, value=point, padx=100, justify='left')


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


root.mainloop()

你是巨猪怪_ 发表于 2020-4-1 20:19:22

zltzlt 发表于 2020-4-1 20:13


{:5_106:}{:5_106:}{:5_106:}
大哥你是真的强什么时候我也能像你一样就好了

你是巨猪怪_ 发表于 2020-4-1 20:50:59

zltzlt 发表于 2020-4-1 20:13


大哥,answer有5个值,answer有6个值,answer只显示了5个,没有完全显示出来,怎么办{:10_254:}
页: [1]
查看完整版本: 我想实现Ratiobutton更新,结果失败,求解