Tkinter,python 3.8.1,win10Pro,如何更改标签图像? [英] Tkinter, python 3.8.1, win10Pro, How can I change a label image?

查看:72
本文介绍了Tkinter,python 3.8.1,win10Pro,如何更改标签图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以请原谅我.我正在尝试制作骰子滚动游戏的GUI(2 x六面).随机滚动的逻辑可以很好地用于控制台.同样在控制台中,我看到模具编号映射到正确的图像文件,但是在超出初始启动卷的每个卷上,我都无法将tkinter标签图像更改为新的对应图像.

This is my VERY first post so forgive my newness. I'm trying to make a GUI of a dice rolling game (2 x six-sided). Logic of the random rolls is working fine to console. Also in console I see that die number is mapping to correct image file, but I'm having trouble changing tkinter label images to the new corresponding images on every roll beyond the initial startup roll.

在启动时,它会正确显示两个模具图像,但是当我单击滚动"按钮时,来自第一个辊的两个图像都消失了,并且不显示新的辊图像.它只是使第一张滚动图像先前占用的空间空白.

On startup, it displays both die images correctly, but when I click "roll" button, both images from first roll just disappear and new roll images are not displayed. It just blanks the space previously occupied by first roll images.

仔细观察,我可以在屏幕上的正确位置看到正确的模具图像闪光",但是每次按滚动"时,这些图像立即消失.

Looking closely, I can see the correct die images "flash" on screen in their correct positions only to immediately disappear each time I press "roll".

我无法附加我正在使用的六张图像以进行可能的模版轧制(缺乏信誉),但要点是要展示从任何图像更改为任何其他图像的能力,因此可以随意尝试使用任何6张gif文件

I'm unable to attach the six images I'm using for the possible die rolls (lack of creds), but point is to demonstrate ability to change from any image to any other so feel free to try with any 6 gifs.

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

I saw similar questions on this site but when I tried code suggested, or combos of code suggested, I had same problem I am having now.

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

I'm using python 3.8.1 on win10pro. Any help would be appreciated! Here's my code:

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

推荐答案

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

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

这篇关于Tkinter,python 3.8.1,win10Pro,如何更改标签图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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