Python - Cómo reemplazar una imagen en tkinter

 
Vista:

Cómo reemplazar una imagen en tkinter

Publicado por Elias (1 intervención) el 19/05/2020 06:42:52
Estoy creando un juego de ahorcado que tengo prácticamente hecho, mi único problema son la transición de las imágenes, al principio se puede ver la imagen principal, pero cuando comete un error, debería aparecer otra imagen mostrando el progreso del muñeco, pero se pone la pantalla en blanco, y no muestra las imágenes, más que la primera y la última si haz perdido.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#importaciones
from tkinter import *
from random import randint
from tkinter.messagebox import *
import sys
#funciones
def funcDisney():
    global a
    Label(ahorcado,width = 14, height = 20, bg = "floral white").place(x = 45, y = 65)
    Label(ahorcado, text = "Disney          ", font = ("Comic Sans MS", 18, "bold"), bg = "blue").place(x = 50, y = 20)
    tema_palabras = "Disney.txt"
    txt = open(tema_palabras,"r")
    letrasave = StringVar()
    lista = list(txt.read().split("\n"))
    word = lista[randint(0,(len(lista)-1))].lower()
    letraenter = Entry(ahorcado, width = 1, font = ("Comic Sans MS", 20), textvariable = letrasave).place(x = 390, y = 402)
    vidas = Label(ahorcado, text = intentos, bg = "red1", font = ("fixedsys",24), relief = "raised", bd = 6).place(x = 905, y = 394)
    espacio_letra = [Label(ahorcado, text = "_", font = ("Comic Sans MS", 22), bg = "floral white") for _ in word]
    for i in range(len(word)):
        espacio_letra[i].place(x = a, y = 300)
        a += 50
    def comprobar():
        global intentos
        global aciertos
        global numarchivo
        enter.append(letrasave.get())
        if letrasave.get() in word:
            usadas = Label(ahorcado, text = enter, bg = "floral white", font = ("Comic Sans MS",16, "bold"), relief = "ridge", bd = 4).place(x = 50, y = 500)
            if word.count(letrasave.get())>1:
                aciertos += word.count(letrasave.get())
                for i in range(len(word)):
                    if word[i] == letrasave.get():
                        espacio_letra[i].config(text = "" + letrasave.get())
            else:
                aciertos += 1
                espacio_letra[word.index(letrasave.get())].config(text = "" + letrasave.get())
            if aciertos == len(word):
                showwarning(title = "VICTORIA", message = "Felicidades, lo salvaste!! heroe sin capa, si esto fuera un examen deberian darte un 10")
                sys.exit()
        else:
            usadas = Label(ahorcado, text = enter, bg = "floral white", font = ("Comic Sans MS",16, "bold"), relief = "ridge", bd = 4).place(x = 50, y = 500)
            intentos -= 1
            numarchivo += 1
            vidas = Label(ahorcado, text = intentos, bg = "red1", font = ("fixedsys",24), relief = "raised", bd = 6).place(x = 905, y = 394)
            imagen = PhotoImage(file = "ahorcado" + str(numarchivo) + ".png")
            Label(root, image = imagen).place(x = 340, y = 50)
            if intentos == 0:
                vidas = Label(ahorcado, text = ":(", bg = "red1", font = ("fixedsys",24), relief = "raised", bd = 6).place(x = 905, y = 394)
                showwarning(title = "GAME OVER", message = "No te quedan vidas dummy, intentalo de nuevo :(")
                sys.exit()
    boton_comprobar = Button(ahorcado, text = "Probar suerte x(", bg = "gold" , font = ("Comic Sans MS", 16), command = comprobar).place(x = 440, y = 401)
 
root = Tk()
root.config(cursor = "pencil", width = 1100, height = 700, bg = "gold", relief = "raised", bd = 25)
ahorcado = Frame(root)
ahorcado.config(width = 1100, height = 700, bg = "floral white", relief = "sunken", bd = 30)
ahorcado.grid_propagate(False)
ahorcado.pack()
imagen = PhotoImage(file = "ahorcado1.png")
Disney = PhotoImage(file = "Disney.png")
Label(root, image = imagen).place(x = 340, y = 50)
Label(ahorcado, text = "Vidas restantes <3 :", font = ("Comic Sans MS", 18, "bold")).place(x = 650, y = 404)
Label(ahorcado, text = "Ingresa una Letra: ", font = ("Comic Sans MS", 24, "bold")).place(x = 50, y = 400)
Label(ahorcado, text = "Letras usadas:", font = ("Comic Sans MS", 18, "bold")).place(x = 50, y = 455)
Label(ahorcado, text = "Elige un Tema:", font = ("Comic Sans MS", 18, "bold")).place(x = 50, y = 20)
#variables
a = 275
intentos = 8
aciertos = 0
numarchivo = 1
enter = []
#Procesos
boton_Disney = Button(ahorcado, image = Disney, command = funcDisney ).place(x = 50, y = 70)
 
root.mainloop()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder