mercredi 6 juin 2018

Python - tkinter, button click with if Statement for a specific pictur in a label

im trying to do a quiz with 4 pictures given randomly (only one at a time in a label). Below there are 4 buttons which are the answer options. The buttons always stay the same, just the pictures change randomly after answering the right question. So when there is a picture, after pressing the right button the picture should change into another one. If pressing the wrong button, nothing should happen.

Here is my code so far the problem is at referring to the picture which is seen at the moment in the right way.

Right now i tried to change the picture into the next one after answering correct, because i didnt know how to insert a random change.

Thanks for every help!

from tkinter import *
from random import randint

Fenster = Tk()
Fenster.title('training')
Fenster.geometry('1024x720')

# images
img110 = PhotoImage(file='1.gif')
img120 = PhotoImage(file='2.gif')
img130 = PhotoImage(file='3.gif')
img140 = PhotoImage(file='4.gif')

# Label image
bild=randint(1,4)
if bild==1:
    labelbild = Label(image=img110)
elif bild==2:
    labelbild = Label(image=img120)
elif bild==3:
    labelbild = Label(image=img130)
elif bild==4:
    labelbild = Label(image=img140)
labelbild.place(x=350, y=150)

#actions

def button110Click():
    if  bild==1:
        labelbild.config(image=img120)
    else:
        pass

def button120Click():
    if bild==2:
        labelbild.config(image=img130)
    else:
        pass

def button130lick():
    if bild==3:
        labelbild.config(image=img140)
    else:
        pass

def button140Click():
    if bild==4:
        labelbild.config(image=img110)
    else:
        pass


# Buttons
button110 = Button(master=Fenster, text='108', bg='#D5E88F', command=button110Click)
button110.place(x=350, y=420, width=40, height=40)
button120 = Button(master=Fenster, text='120', bg='#FFCFC9', command=button120Click)
button120.place(x=440, y=420, width=40, height=40)
button130 = Button(master=Fenster, text='128.57', bg='#FBD975', command=button130Click)
button130.place(x=530, y=420, width=40, height=40)
button140 = Button(master=Fenster, text='135', bg='#FBD975', command=button140Click)
button140.place(x=620, y=420, width=40, height=40)


Fenster.mainloop()

Aucun commentaire:

Enregistrer un commentaire