tkinter:将窗口中的图像从变量更改为随机图像 [英] tkinter: change image in window to random image from variable

查看:69
本文介绍了tkinter:将窗口中的图像从变量更改为随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个骰子,所以每个图像都是骰子一张脸的图片,我希望它使用按钮随机选择其中一个图像,然后将其打印在屏幕上的图像替换为另一个随机图像单击按钮时

I'm trying to make a dice so each image is the picture of a face of a dice and I want it to randomly pick one of the images using a button then replace the image its printed on the screen with another random image when the button is click

因此,当我运行模块时,它会弹出6张随机图像之一,但是当我单击滚动"按钮时,它不会将图像更改为另一张随机图像,而是使图像消失了.我希望变量"myimg"根据"number"变量更改其值,然后出来使用按钮将tkinter窗口上打印的图像替换为已知的"myimg"值.

So when I run the module it pops up with one of the 6 random images but as soon as i click the button 'roll' it doesn't change the image to another random image but makes the image disappear. I'm wanting the variable 'myimg' to change its value depending on the 'number' variable, out come then replaces the image printed on the tkinter window to then known 'myimg' value by using the button.

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

推荐答案

您的while循环无法正常运行.执行代码时,除非明确调用,否则不会再次执行.将计数重置为1不会这样做.因此,您应该定义一个选择要显示的新图像的函数,而不是使用while循环.试试这个:

Your while loop does not work as you expect it to. When code is executed it does not execute again unless specifically called. Resetting count to 1 does not do that. So instead of using that while loop, you should define a function which picks a new image to show. Try this:

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

这篇关于tkinter:将窗口中的图像从变量更改为随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆