我试图制作一个骰子,因此每个图像都是骰子的图像,我希望它使用一个按钮随机选择一个图像,然后用另一个图像替换其在屏幕上打印的图像单击按钮时的随机图像
因此,当我运行该模块时,它会弹出6个随机图像中的一个,但只要我点击按钮“滚动”即可。它不会将图像更改为另一个随机图像,但会使图像消失。我想要变量' myimg'根据'来改变其价值。变量,out然后将tkinter窗口上打印的图像替换为已知的“myimg'使用按钮的价值。
import random
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Dice")
window.geometry("400x400")
window.configure(background='white')
count= 1
while count == 1:
number = random.randint(1,6)
if number == 1:
myimg = "1.jpg"
count = 0
elif number == 2:
myimg = "2.jpg"
count = 0
elif number == 3:
myimg = "3.jpg"
count = 0
elif number == 4:
myimg = "4.jpg"
count = 0
elif number == 5:
myimg = "5.jpg"
count = 0
elif number == 6:
myimg = "6.jpg"
count = 0
def update_the_picture():
updated_picture = ImageTk.PhotoImage(Image.open(myimg))
w.configure(image = updated_picture)
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expect an image object.
img = ImageTk.PhotoImage(Image.open(myimg))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
w = tk.Label(window, image = img)
b = tk.Button(window, text="Role Dice", command = update_the_picture)
#The Pack geometry manager packs widgets in rows or columns.
w.pack(side = "bottom", fill = "both", expand = "yes")
b.pack()
count= 1
#Start the GUI
window.mainloop()
答案 0 :(得分:2)
你的while循环不能像你期望的那样工作。执行代码时,除非特别调用,否则不再执行。将计数重置为1不会这样做。因此,您应该定义一个选择要显示的新图像的函数,而不是使用while循环。试试这个:
import random
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Dice")
window.geometry("400x400")
window.configure(background='white')
def new_img():
# Pick a new number
number = random.randint(1,6)
# Add '.jpg' to number
myimg = str(number)+'.jpg'
# Return the image name
return myimg
def update_the_picture():
# Get the new image name
myimg = new_img()
updated_picture = ImageTk.PhotoImage(Image.open(myimg))
w.configure(image = updated_picture)
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expect an image object.
myimg = new_img()
img = ImageTk.PhotoImage(Image.open(myimg))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
w = tk.Label(window, image = img)
b = tk.Button(window, text="Role Dice", command = update_the_picture)
#The Pack geometry manager packs widgets in rows or columns.
w.pack(side = "bottom", fill = "both", expand = "yes")
b.pack()
#Start the GUI
window.mainloop()
答案 1 :(得分:0)
请注意,图像“updated_picture”是函数的本地图像,因此只要函数update_the_picture()退出就会进行垃圾收集,并且速度足够快,看起来似乎没有显示。你必须使图像持久化。一种方法是打开它们一次并将它们存储在一个列表中,而不是每次调用该函数时。
window = tk.Tk()
window.title("Dice")
window.geometry("400x400")
window.configure(background='white')
def update_the_picture():
num=random.randint(0, 5)
w.configure(image = images[num])
images=[]
for fname in range(1,7):
img = ImageTk.PhotoImage(Image.open("%d.jpg" % (fname)))
images.append(img)
w = tk.Label(window, image = img)
w.pack(side = "bottom", fill = "both", expand = "yes")
b = tk.Button(window, text="Role Dice", command = update_the_picture).pack()
update_the_picture()
window.mainloop()