Tkinter, python 3.8.1, win10Pro, 如何更改标签图片?

Tkinter, python 3.8.1, win10Pro, How can I change a label image?

这是我的第一个 post 所以请原谅我的新奇。我正在尝试制作掷骰子游戏的 GUI(2 x 六面)。随机掷骰的逻辑在控制台上运行良好。同样在控制台中,我看到模具编号映射到正确的图像文件,但我无法将 tkinter 标签图像更改为初始启动卷之外的每个卷上的新对应图像。

启动时,它会正确显示两个模具图像,但是当我单击 "roll" 按钮时,第一卷的两个图像都消失了,并且没有显示新的卷图像。它只是空白 space 先前被第一卷图像占用。

仔细观察,我可以在屏幕上的正确位置看到正确的骰子图像 "flash",但每次我按下 "roll" 时它们都会立即消失。

我无法附上我用于可能的掷骰子的六张图片(缺乏可信度),但重点是展示从任何图片更改为任何其他图片的能力,所以请随意尝试任何图片6 张动图。

我在这个网站上看到了类似的问题,但是当我尝试建议的代码或建议的代码组合时,我遇到了与现在相同的问题。

我在 win10pro 上使用 python 3.8.1。任何帮助,将不胜感激!这是我的代码:

from tkinter import *
import random

window = Tk()
window.title( 'Roller' )
window.resizable( 0, 0 )

def get_roll():
    min=1
    max=6

    die1 = random.randint(min,max)
    die2 = random.randint(min,max)

    if die1 == die2:
        print(die1,'+',die2,'=',die1+die2, '*** You rolled doubles ***')
    else:    
        print(die1,'+',die2,'=',die1+die2)
    return die1,die2

def get_image(index):
    images = []
    images.append('die_01_42158_sm.gif')
    images.append('die_02_42159_sm.gif')
    images.append('die_03_42160_sm.gif')
    images.append('die_04_42161_sm.gif')
    images.append('die_05_42162_sm.gif')
    images.append('die_06_42164_sm.gif')
    return images[index-1]

def do_roll():
    global window

    die1, die2 = get_roll()

    imgfile1 = get_image(die1)
    imgfile2 = get_image(die2)

    print(imgfile1)
    img1 = PhotoImage( file = imgfile1 )
    #img1 = img1.subsample(20)
    imgLbl1.configure( image = img1 )
    #imgLbl1 = Label( window, image = img1 )
    #imgLbl1.grid(row = 0, column = 0)
    window.update_idletasks()

    print(imgfile2)
    img2 = PhotoImage( file = imgfile2 )
    #img2 = img2.subsample(20)
    imgLbl2.configure( image = img2 )
    #imgLbl2 = Label( window, image = img2 )
    #imgLbl2.grid(row = 0, column = 1)
    window.update_idletasks()

die1, die2 = get_roll()
imgfile1 = get_image(die1)
imgfile2 = get_image(die2)

img1 = PhotoImage( file = imgfile1 )
#img1 = img1.subsample(20)
imgLbl1 = Label( window, image = img1 )
imgLbl1.grid( row = 0, column = 0 )

img2 = PhotoImage( file = imgfile2 )
#img2 = img2.subsample(20)
imgLbl2 = Label( window, image = img2 )
imgLbl2.grid( row = 0, column = 1 )

rollBtn = Button( window )
rollBtn.grid( row = 0, column = 2 )
rollBtn.configure( text = 'Roll' )
rollBtn.configure( command = do_roll )

quitBtn = Button( window )
quitBtn.grid( row = 0, column = 3 )
quitBtn.configure( text = 'Quit' )
quitBtn.configure( command = window.destroy )

#do_roll()

window.mainloop()

由于您使用局部变量来保存图像,它们将在函数后被垃圾回收。

您必须保留图片的参考资料:

def do_roll():
    die1, die2 = get_roll()

    imgfile1 = get_image(die1)
    imgfile2 = get_image(die2)

    print(imgfile1)
    imgLbl1.image = PhotoImage(file=imgfile1)
    imgLbl1.configure(image=imgLbl1.image)

    print(imgfile2)
    imgLbl2.image = PhotoImage(file=imgfile2)
    imgLbl2.configure(image=imgLbl2.image)

或将 img1img2 声明为全局:

def do_roll():
    global img1, img2

    die1, die2 = get_roll()

    imgfile1 = get_image(die1)
    imgfile2 = get_image(die2)

    print(imgfile1)
    img1 = PhotoImage(file=imgfile1)
    imgLbl1.configure(image=img1)

    print(imgfile2)
    img2 = PhotoImage(file=imgfile2)
    imgLbl2.configure(image=img2)

使用上面的 acw1668 解决方案,我能够以此为基础来完成我模拟掷一对骰子的目标。最初,骰子经过 10 次随机投掷,然后在第十次停止,即 'roll'。随后每次按下滚动按钮都会导致相同的结果。我期望的编程目标是证明标签图像可以多次更改。这是代码(但您必须为骰子的 6 个面提供您自己的图像 - 抱歉,信用低无法下载):

from tkinter import *
import random

def get_roll():
    min=1
    max=6

    die1 = random.randint(min,max)
    die2 = random.randint(min,max)

    if die1 == die2:
        print(die1,'+',die2,'=',die1+die2, '*** You rolled doubles ***')
    else:    
        print(die1,'+',die2,'=',die1+die2)
    return die1,die2

def get_image(index):
    images = []
    images.append('die_01_42158_sm.gif')
    images.append('die_02_42159_sm.gif')
    images.append('die_03_42160_sm.gif')
    images.append('die_04_42161_sm.gif')
    images.append('die_05_42162_sm.gif')
    images.append('die_06_42164_sm.gif')
    return images[index-1]

counter = 0 
def counter_label():
    global counter
    print('counter_label() counter =', counter)
    def count():
        global counter, imgLbl1, imgLbl2

        print('count() counter =', counter)

        print(counter)
        counter += 1
        if counter > 10:
           return

        die1, die2 = get_roll()

        imgfile1 = get_image(die1)
        imgLbl1.image = PhotoImage( file = imgfile1 )
        imgLbl1.configure( image = imgLbl1.image )

        imgfile2 = get_image(die2)
        imgLbl2.image = PhotoImage( file = imgfile2 )
        imgLbl2.configure( image = imgLbl2.image )

        imgLbl1.after(10, count)

    if counter >= 10:
        counter = 0
    count()

root = Tk()
root.title("Counting Seconds")

imgLbl1 = Label(root)
imgLbl1.pack(side =LEFT)
imgLbl2 = Label(root)
imgLbl2.pack(side =LEFT)

counter_label()

buttonr = Button(root, text='Roll', width=25, command=counter_label)
buttonr.pack()

buttonq = Button(root, text='Stop', width=25, command=root.destroy)
buttonq.pack()

root.mainloop()