首页 > 解决方案 > Tkinter,python 3.8.1,win10Pro,如何更改标签图像?

问题描述

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

启动时,它正确显示两个模具图像,但是当我单击“滚动”按钮时,第一个滚动的两个图像都消失了,并且不显示新的滚动图像。它只是清空了先前由第一卷图像占据的空间。

仔细观察,我可以看到正确的模具图像在屏幕上的正确位置“闪烁”,但每次按“滚动”时立即消失。

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

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

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

标签: python-3.ximagetkinterlabel

解决方案


Using acw1668's solution above I was able to build upon that to complete my goal of simulating rolls of a pair of dice. Initially, the dice go through 10 random tosses then stop at the tenth as the 'roll'. Each subsequent press of the roll button causes same. My desired programmatic goal was to demonstrate that a label image could be changed multiple times. Here's the code (but you'll have to provide your own images for the 6 sides of the die - sorry, can't download with low creds):

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

推荐阅读