如何在Python 3

时间:2017-10-01 21:26:51

标签: image python-3.x random tkinter

我应该事先承认我正在学习python,因为我去使用谷歌作为我的主要老师。我感谢您提供的任何帮助或建议!

我正在编写一个播放声音的程序,然后为用户提供四个图像选项供您选择。我遇到的问题是四个图像位置的随机化。我编写了一个程序,它将随机化4个标准文本选项的顺序,然后在事实之后显示正确的图像,我有一个程序将显示四个随机图像,但正确的图像始终是第四个图像。我可以使用随机函数或某种随机函数重新排列每次显示图像的顺序吗?

以下显示位于相同位置的图像的程序代码。注意,我的下一步是让程序识别何时按下右键,现在它只是重新显示相同的图像,但这次不是我的问题。谢谢你的帮助!

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)

1 个答案:

答案 0 :(得分:0)

没有人回应解决方案,但我想我已经能够回答我自己的问题了。我最终创建了一个按钮列表,然后使用shuffle重新组织列表。从那里,我只是使用“.grid”来显示新的,现在随机化的列表顺序。更新的代码如下。

def button1():
    label = Label(master, image=correctanswer, anchor = S)
    label.image = answer1
    label.grid(row=2, column=1)
    print ("You wrong fool")

def button2():
    label = Label(master, image=correctanswer, anchor = S)
    label.image = answer2
    label.grid(row =2, column=1)
    print ("Try again...")

def button3():
    label = Label(master, image=correctanswer, anchor = S)
    label.image = answer3
    label.grid(row =2, column=1)
    print ("Are you sure this is for you?")

def button4():
    label = Label(master, image=correctanswer, anchor = S)
    label.image = correctanswer
    label.grid(row =3, column=3)
    print ("That's right!")
    print ("The letter is", (Letter.rsplit(".", 1)[0]))
    winsound.PlaySound((Soundpath)+ (Letter), winsound.SND_FILENAME)
    input("Press ENTER to Continue")

    master.destroy()

button1 = Button(master, image=answer1, command= button1)

button2 = Button(master, image=answer2, command= button2)

button3 = Button(master, image=answer3, command= button3)

button4 = Button(master, image=correctanswer, command= button4)

questions = [button1, button2, button3, button4]
random.shuffle(questions)

questions[0].grid(row=0, column=1)
questions[1].grid(row=1, column=1)
questions[2].grid(row=0, column=2)
questions[3].grid(row=1, column=2)

我还在“def按钮...”部分对我的代码进行了一些较小的编辑。