I am building a multiple choice game using Python and tkinter
and I need to be able to shuffle the buttons on the GUI so that the position of the button containing the correct answer changes. I have written this code so far but it seems like the same rely
value is often taken from the y_list
multiple times leading to buttons hiding each other. How is it possible to ensure that each rely
value is only taken once?
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)
Just avoid your randy
function all together and use your y_list
directly after random.shuffle()
it:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.