我试着四处看看,但只发现有人试图改变按钮上的图像,而不是用按钮
这就是我目前所拥有的
from Tkinter import *
import Tkinter as tk
from random import randrange
from PIL import ImageTk, Image
import tkMessageBox
import tkFont
root = tk.Tk()
root.title("Randomizer")
root.minsize(900,600)
root.maxsize(900,600)
adress = "C:\Users\pc\Desktop\ClassPics\Class.png"
playerclass1 = ImageTk.PhotoImage(Image.open(adress))
panel = tk.Label(root, image = playerclass1)
panel.pack(side='top')
def randomizer():
playerclassV = randrange(0,10)
if playerclassV == 0:
adress = "C:\Users\pc\Desktop\ClassPics\1.png"
elif playerclassV == 1:
adress = "C:\Users\pc\Desktop\ClassPics\2.png"
elif playerclassV == 2:
adress = "C:\Users\pc\Desktop\ClassPics\3.png"
elif playerclassV == 3:
adress = "C:\Users\pc\Desktop\ClassPics\4.png"
elif playerclassV == 4:
adress = "C:\Users\pc\Desktop\ClassPics\5.png"
elif playerclassV == 5:
adress = "C:\Users\pc\Desktop\ClassPics\6.png"
elif playerclassV == 6:
adress = "C:\Users\pc\Desktop\ClassPics\7.png"
elif playerclassV == 7:
adress = "C:\Users\pc\Desktop\ClassPics\8.png"
else:
adress = "C:\Users\pc\Desktop\ClassPics\9.png"
playerclass = ImageTk.PhotoImage(Image.open(adress))
panel.configure(image = playerclass)
panel.image = playerclass
Randomize = Button ( root, text="Randomize", justify="center", width=20, command=randomizer())
Randomize.pack(side = 'bottom')
root.mainloop()
这个想法是从一个窗口开始,只有初始图像和随机化按钮,而随机化按钮用随机选择的9个图像中的一个替换初始图像。现在它不显示初始图像,而是直接进入功能(显示后面9张图片中的一张,而不是初始图像),按钮什么也不做
请帮忙
这是因为您正在调用函数。 您应该使用
command=randomizer
。如果不想在运行程序时立即调用函数,请不要使用()
。 如果希望发送一些参数(在本例中显然不是这样),可以使用command=lambda:randomizer(args)
。 请参阅this以获得更详细的答案相关问题 更多 >
编程相关推荐