I should admit upfront that I'm learning python as I go and using google as my primary teacher. I appreciate any help or advise that you all can offer!
I am writing a program that plays a sound and then gives the user four image options to choose from. The issue I have encountered is the randomization of the locations of the four images. I have written a program that will randomize the order of 4 standard text options and then display the correct image after the fact and I have wiritten a program that will display four random images but the correct image is always the 4th image. Can I use the random function or some sort of shuffle function to rearrange the order of the displayed images each time?
The code for the program that displays the images in the same position is below. Note, my next step is to get the program to recognize when the right button is pressed, right now it just re-displays the same image, but that's not my question this time. Thanks for the help!
import os, random
from random import choice
import winsound
import time
from tkinter import *
master=Tk()
Imagepath = "C:\\LearnArabic\\alphabet\\Image\\"
Soundpath = "C:\\LearnArabic\\alphabet\\Sound\\"
Letter = random.choice(os.listdir(Soundpath))
answer1 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
answer2 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
answer3 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
correctanswer = PhotoImage(file=(Imagepath+((Letter.rsplit(".", 1)[0])+ ".png")))
winsound.PlaySound((Soundpath)+ (Letter), winsound.SND_FILENAME)
def button1():
label = Label(master, image=answer1, anchor = S)
label.image = answer1
label.place(x=50, y=100)
def button2():
label = Label(master, image=answer2, anchor = S)
label.image = answer2
label.place(x=100, y=100)
def button3():
label = Label(master, image=answer3, anchor = S)
label.image = answer3
label.place(x=50, y=150)
def button4():
label = Label(master, image=correctanswer, anchor = S)
label.image = correctanswer
label.place(x=100, y=150)
button1 = Button(master, image=answer1, command= button1)
button1.grid(row =2, column=2)
button2 = Button(master, image=answer2, command= button2)
button2.grid(row =3, column=2)
button3 = Button(master, image=answer3, command= button3)
button3.grid(row =2, column=3)
button4 = Button(master, image=correctanswer, command= button4)
button4.grid(row =3, column=3)
Aucun commentaire:
Enregistrer un commentaire