你是巨猪怪_ 发表于 2020-4-1 21:12:16

Radiobutton的替换虽然替换了,但是没有完全替换

answer有5个值,answer有6个值,answer只显示了5个,没有完全显示出来,怎么办

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()

zltzlt 发表于 2020-4-1 21:13:38

貌似只能这样……

from tkinter import *

x = 0

root = Tk()

answers = {0: [('草莓', 2), ('苹果', 3), ('西瓜', 5), ('菠萝', 10), ('橘子', 15), ('', 20)],
         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 21:19:37

zltzlt 发表于 2020-4-1 21:13
貌似只能这样……

那如果是有三个值的呢?
比如再来一个   3: [('草莓', 2), ('苹果', 3), ('西瓜', 5)]

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

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

那就在列表后面再加几个 ('', 10)

你是巨猪怪_ 发表于 2020-4-1 21:21:41

zltzlt 发表于 2020-4-1 21:13
貌似只能这样……

我明白了,但是没有别的办法把多余项删掉吗{:10_254:}

zltzlt 发表于 2020-4-1 21:23:53

你是巨猪怪_ 发表于 2020-4-1 21:21
我明白了,但是没有别的办法把多余项删掉吗

删除组件好像可以用组件的 destroy() 方法

你是巨猪怪_ 发表于 2020-4-1 21:32:49

zltzlt 发表于 2020-4-1 21:23
删除组件好像可以用组件的 destroy() 方法

如果用destroy()的话不就等于把choices1删除了嘛,那么answer也放不到choices1里了,还是说要建立一个新的组件放answer?

zltzlt 发表于 2020-4-1 21:34:01

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

应该是要为 answer、answer 的每一个元素都建立一个 Radiobutton 组件 ,而不只是 config()

你是巨猪怪_ 发表于 2020-4-1 21:40:47

zltzlt 发表于 2020-4-1 21:34
应该是要为 answer、answer 的每一个元素都建立一个 Radiobutton 组件 ,而不只是 config()

请大神指点,我试了还是改不好{:10_266:}
页: [1]
查看完整版本: Radiobutton的替换虽然替换了,但是没有完全替换