为什么我无法获得Radiobutton的价值?

时间:2018-01-23 15:07:59

标签: python variables tkinter

我想获得我的Radiobutton的价值,但它不起作用。这可能是因为我试图获得一本字典。这是我的代码:

vars = []
playersg={"Bob":"1", "Jeff":"2", "John":"3", "Adam":"4"}


def NextRound():
    j = 0
    playersg2 = []
    while j < len(vars):
        playersg2 += vars[j].get()
        print(j)
        print(vars[j].get())
        j += 1
    print(playersg2)

def match():
    matchw = Tk()
    matchw.withdraw()

    draw = 0

    vieww.withdraw()
    matchw.deiconify()
    matchw.title('Tournament Software')
    matchw.geometry("300x999900")
    matchw.configure(background="#1aff29")

    numPlayers = len(playersg)
    numDraws = numPlayers/2
    matches=Label(matchw, text="Matches", font="none 50 bold", bg="#1aff29", fg="black").pack()

    while draw < numDraws:
        frame = Frame(matchw, bg="#1aff29")
        frame.pack()

        var = StringVar()
        R1=Radiobutton(frame, text=playersg[draw], bg="#00e60f" , font="none 10 bold", fg="black",
                       command=NextRound, indicatoron = 0,
                       value = playersg[draw], variable=var)
        R1.pack(side=LEFT, anchor=W)
        Label(frame, text = "vs", font = "none 10 bold", bg="#1aff29", fg="black").pack(side=LEFT, anchor=W)
        R2=Radiobutton(frame, text=playersg[numPlayers-draw-1] , bg="#00e60f", font="none 10 bold", fg="black",
                       command=NextRound, indicatoron = 0,
                       value = playersg[numPlayers-draw-1], variable=var)
        R2.pack(side=LEFT, anchor=W)

        draw += 1
        print(var.get())
        vars.append(var)

没有错误,但是当我print(var[j].get())时,它不会打印任何内容。

2 个答案:

答案 0 :(得分:0)

draw尝试使用playersg的密钥访问值,这是一个数字,而playersg={"Bob":"1", "Jeff":"2", "John":"3", "Adam":"4"} 中没有数字键。

替换:

playersg = ["Bob", "Jeff", "John", "Adam"]

使用:

playersg

使def fun(item): return [item[item1]+' '+k for k in item[item2]] res = [] [res.append(fun(i)) for i in some_list if(fun(i)) not in res] print res 成为列表而不是字典。

答案 1 :(得分:0)

我找到了答案,您需要将窗口作为StringVar()的参数。如果您没有放置所需的窗口,默认情况下它将使用主窗口。对于我的情况,它应该是这样的

var = StringVar(matchw)