【问题标题】:Tkinter, python 3.8.1, win10Pro, How can I change a label image?Tkinter,python 3.8.1,win10Pro,如何更改标签图像?
【发布时间】:2020-02-20 04:08:09
【问题描述】:

这是我的第一篇文章,所以请原谅我的新鲜感。我正在尝试制作掷骰子游戏的 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.x image tkinter label


    【解决方案1】:

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

    您必须保留图片的引用:

    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,非常感谢您迅速、准确的回复!我测试了它,它完全按照你的指示工作!
    • 由于我刚从这里开始并且信用很少,我能够标记答案已检查,但我无法增加计数器。也许这里的其他好公民可以更新它,因为 acw1668 获得了出色的工作......
    【解决方案2】:

    使用上述 acw1668 的解决方案,我能够在此基础上完成模拟掷骰子的目标。最初,骰子经过 10 次随机掷骰,然后在第十次“掷骰”时停止。随后每次按下滚动按钮都会导致相同的结果。我期望的程序化目标是证明标签图像可以多次更改。这是代码(但您必须为骰子的 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多