【问题标题】:tkinter: change image in window to random image from variabletkinter:将窗口中的图像更改为变量中的随机图像
【发布时间】:2015-01-12 12:36:40
【问题描述】:

我正在尝试制作骰子,因此每张图片都是骰子面的图片,我希望它使用按钮随机选择其中一张图片,然后将其打印在屏幕上的图片替换为另一张随机图片当按钮被点击时

因此,当我运行该模块时,它会弹出 6 个随机图像之一,但是一旦我单击“滚动”按钮,它就不会将图像更改为另一个随机图像,而是使图像消失。我希望变量“myimg”根据“数字”变量更改其值,然后使用按钮将打印在 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()

【问题讨论】:

    标签: python image tkinter


    【解决方案1】:

    您的 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()
    

    【讨论】:

      【解决方案2】:

      请注意,图像“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()
      

      【讨论】:

        猜你喜欢
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多