我试着用随机给出的4张图片做一个测试(在一个标签中一次只能有一张)。下面有4个按钮是答案选项。按钮总是保持不变,只是回答正确问题后图片会随机变化。所以当有图片时,按右键后,图片应该会变成另一张。如果按错了按钮,什么也不会发生。在
到目前为止,问题是指的是以正确的方式看到的画面。在
现在,我试着在回答正确后把图片换成下一张,因为我不知道如何插入一个随机的改变。在
谢谢你的帮助!在
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()
我建议使用一个列表来保存图像,并使用
bild
作为这个列表的索引。在因为我没有tkinter,所以我无法测试整个应用程序,所以可能有bug。在
相关问题 更多 >
编程相关推荐