【问题标题】:Shuffling buttons on tkinter GUItkinter GUI 上的随机播放按钮
【发布时间】:2019-12-28 11:37:35
【问题描述】:

我正在使用 Python 和 tkinter 构建一个多项选择游戏,我需要能够随机播放 GUI 上的按钮,以便包含正确答案的按钮的位置发生变化。到目前为止,我已经编写了这段代码,但似乎相同的 rely 值经常取自 y_list 多次导致按钮相互隐藏。如何确保每个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)

【问题讨论】:

    标签: python tkinter random shuffle


    【解决方案1】:

    只是避免你的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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多