Python 3.5.0 ::: Delete a Label in Tkinter

Asked

Viewed 1,748 times

2

I have a code that allows me to play to stone-paper-scissors. Here’s the Tkinter window:

inserir a descrição da imagem aqui

The result does not appear every time I play, because the label of this text does not disappear or delete the text. How do I get to see the result every time I play.

Here’s the code:

#JOGO PEDRA-PAPEL-TESOURA
#TKINTER MODULE

from tkinter import *
import random
ppt = ['pedra', 'papel', 'tesoura']
res = 'JOGAR'

root = Tk()
root.geometry('800x600')
root.title('ROCK - PAPER - SCISSORS')

frame_texto = Frame(root)
frame_texto.pack(side=BOTTOM)
frame_texto.place(height=100, width=200, x=300, y=300)

def res_texto(res):
    texto = Label(frame_texto, text= res, fg='red', font=('Times New Roman', 40))
    texto.pack()

def jogar_pedra(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'EMPATE'
        res_texto(res)
    elif x == 'papel':
        res = 'PERDEU'
        res_texto(res)
    elif x == 'tesoura':
        res = 'GANHOU'
        res_texto(res)

def jogar_papel(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'GANHOU'
        res_texto(res)
    elif x == 'papel':
        res = 'EMPATE'
        res_texto(res)
    elif x == 'tesoura':
        res = 'PERDEU'
        res_texto(res)

def jogar_tesoura(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'PERDEU'
        res_texto(res)
    elif x == 'papel':
        res = 'GANHOU'
        res_texto(res)
    elif x == 'tesoura':
        res = 'EMPATE'
        res_texto(res)



instrucoes = Label(root, text='Escolha pedra, papel ou tesoura', font = ('Times New Roman', 20), fg='black')
instrucoes.pack()

pedra = Button(root, text='Pedra', font=(30))
pedra.bind('<Button-1>', jogar_pedra)
pedra.pack()
pedra.place(x=250, y=100)

papel = Button(root, text='Papel', font=(30))
papel.bind('<Button-1>', jogar_papel)
papel.pack()
papel.place(x= 350, y=100)

tesoura = Button(root, text='Tesoura', font=(30))
tesoura.bind('<Button-1>', jogar_tesoura)
tesoura.pack()
tesoura.place(x=450, y=100)

root.mainloop()

2 answers

3


Before I move on to your concrete problem, I wanted to say that place is not widely used or is mostly used in specific cases. I for example almost never used place, and normally use a combination of grid and pack, and believes that once you understand how these work, you will rarely need to place. Conclusion, know how to use grid and pack well helps you in almost every situation where you have to create several layouts.

Apart from this advice, your problem is that when you call the job res_texto you continue to create a Label and to "pack it up". The advice is that you create that Label once and successively you modify only her text.

#JOGO PEDRA-PAPEL-TESOURA
#TKINTER MODULE

from tkinter import *
import random


ppt = ['pedra', 'papel', 'tesoura']
res = 'JOGAR'

root = Tk()
root.geometry('800x600')
root.title('ROCK - PAPER - SCISSORS')

frame_texto = Frame(root)
frame_texto.pack(side=BOTTOM)
frame_texto.place(height=100, width=200, x=300, y=300)

# Creias 1 vez a label
texto = Label(frame_texto, text= res, fg='red', font=('Times New Roman', 40))
texto.pack()

# Aqui só modificas o texto da Label texto
def res_texto(res):
    texto.config(text=res)

def jogar_pedra(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'EMPATE'
        res_texto(res)
    elif x == 'papel':
        res = 'PERDEU'
        res_texto(res)
    elif x == 'tesoura':
        res = 'GANHOU'
        res_texto(res)

def jogar_papel(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'GANHOU'
        res_texto(res)
    elif x == 'papel':
        res = 'EMPATE'
        res_texto(res)
    elif x == 'tesoura':
        res = 'PERDEU'
        res_texto(res)

def jogar_tesoura(event):
    x = random.choice(ppt)
    if x == 'pedra':
        res = 'PERDEU'
        res_texto(res)
    elif x == 'papel':
        res = 'GANHOU'
        res_texto(res)
    elif x == 'tesoura':
        res = 'EMPATE'
        res_texto(res)


instrucoes = Label(root, text='Escolha pedra, papel ou tesoura', font = ('Times New Roman', 20), fg='black')
instrucoes.pack()

pedra = Button(root, text='Pedra', font=(30))
pedra.bind('<Button-1>', jogar_pedra)
pedra.pack()
pedra.place(x=250, y=100)

papel = Button(root, text='Papel', font=(30))
papel.bind('<Button-1>', jogar_papel)
papel.pack()
papel.place(x= 350, y=100)

tesoura = Button(root, text='Tesoura', font=(30))
tesoura.bind('<Button-1>', jogar_tesoura)
tesoura.pack()
tesoura.place(x=450, y=100)

root.mainloop()

0

Cara has a way of "deleting " using . place YES simply add a position that is not visible in your window, for example:

jan = Tk()
jan.geometry("500x400")

nome = Label(jan, text="Ganhou")
nome.place(x=200,  y=200)

def ok():

 nome.place(x=600, y=500)
bt = Button(jan, text="OK", command=ok)
bt.place(x=200, y=250)

Notice that I have placed a position that does not exist in my window and to n perceive in any way even when opening the window add an absurd position ex: (x=2000, y=4000)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.