tkinter GUI 上的洗牌按钮

Shuffling buttons on tkinter GUI

我正在使用 Python 和 tkinter 构建一个多项选择游戏,我需要能够随机排列 GUI 上的按钮,以便包含正确答案的按钮的位置发生变化。到目前为止,我已经编写了这段代码,但似乎经常从 y_list 中多次获取相同的 rely 值,导致按钮相互隐藏。如何确保每个 rely 值只被取一次?

y_list=[0.2,0.4,0.6,0.8]

def randy():
    xan = random.choice(y_list)
    return xan

y_list.remove(xan)


wordLabel = Label(newWindow, text=all_words_list[randWord])
wordLabel.place(relx=0.49, rely=0.1)
choice1=Button(newWindow, text=all_definitions_list[randDefinition], height=5, width=20)
choice1.place(relx=0.5,rely=randy(), anchor=N)
choice2=Button(newWindow, text="gangsta", height=5, width=20)
choice2.place(relx=0.5, rely=randy(), anchor=N)
choice3=Button(newWindow, text="gangsta", height=5, width=20)
choice3.place(relx=0.5, rely=randy(), anchor=N)
choice4=Button(newWindow, text="gangsta", height=5, width=20)
choice4.place(relx=0.5, rely=randy(), anchor=N)

只需避免使用 randy 函数,直接在 random.shuffle() 之后使用 y_list 即可:

from random import shuffle

y_list = [0.2, 0.4, 0.6, 0.8]

shuffle(y_list)

wordLabel = Label(newWindow, text=all_words_list[randWord])
wordLabel.place(relx=0.49, rely=0.1)

choice1=Button(newWindow, text=all_definitions_list[randDefinition],
    height=5, width=20)
choice1.place(relx=0.5, rely=y_list[0], anchor=N)

choice2=Button(newWindow, text="gangsta", height=5, width=20)
choice2.place(relx=0.5, rely=y_list[1], anchor=N)

choice3=Button(newWindow, text="gangsta", height=5, width=20)
choice3.place(relx=0.5, rely=y_list[2], anchor=N)

choice4=Button(newWindow, text="gangsta", height=5, width=20)
choice4.place(relx=0.5, rely=y_list[3], anchor=N)