Python 3.5.0 ::: Erase a Label on Tkinter

3

I have a code that allows me to play the stone-paper-scissors. Here is the tkinter window:

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

Here is 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()
    
asked by anonymous 10.01.2016 / 18:02

2 answers

3

Before going to your concrete problem, I wanted to say that place is not very used or is mostly used in specific cases. I for example almost never used place , and I usually use a combination of grid and pack , and believe that once you figure out how they work there, you'll rarely need place . Conclusion, knowing how to use grid and pack will help you almost in every situation where you have to create various layouts.

Taking this advice, your problem is that when you call the res_texto function you continue to create a Label and "pack it". The advice is that you create this Label once and successively modify only the text of it.

#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()
    
10.01.2016 / 22:30
0

Dude has a way to "delete" using .place SIM it is enough that you after an action 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 placed a position that does not exist in my window   and to not notice at all even when opening the window add   an absurd position ex: (x = 2000, y = 4000)

    
13.08.2018 / 16:20