Home > Software engineering >  Python Tkinter - how to reset text
Python Tkinter - how to reset text

Time:10-01

Wanted to know from the below how I can reset the text on the interface, once I click to pick a name again to reset and pick a new one, by pressing the Pick the associate.

root = Tk()
root.title('Pick the MOM')
root.geometry("500x500")

def pick():
    entries = ["Giovanni", "Francesca", "Martin", "Anderson", "Raj" , "Kinga"]
    
    #convert to a set
    entries_set = set (entries)
    #convert back to list 
    unique_entries = list (entries_set)
    
    #create a random number 
    our_number = Len(unique_entries) - 1
    rando=randint(0, our_number)
    
    winnerLabel = Label (root, text=unique_entries[rando], font=('helvetica', 18))
    winnerLabel.pack(pady=50)

topLabel=Label(root, text="MOM Taker", font=('Helvetica', 24))
topLabel.pack(pady=20)

winButton=Button(root, text="Pick the Associate", font=("Helvetica", 24), command=pick)
winButton.pack(pady=20)

resetButton=Button(root, text="Clear", font=("Helvetica", 24))
resetButton.pack(pady=20)


root.mainloop()

enter image description here

CodePudding user response:

There are many modifications that can be made to the above code to optimise it or make it more pythonic, however the solution to your problem (the first solution that comes to mind for me) is to create a child class which can have changeable text. I once had a similar issue with making moveable buttons which could be solved easily with a child class.

Something like so may help:

from tkinter import *
from random import choice

root = Tk()
root.title('Pick the MOM')
root.geometry("500x500")

class ChangingButton(Button):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.configure(command=self.pick)

    def pick(self):
        entries = ["Giovanni", "Francesca", "Martin", "Anderson", "Raj", "Kinga"]
        random_entry = choice(list(set(entries)))
        self.configure(text=random_entry)
        self.configure(font=("helvetica", 18))
        self.configure(command="")
        self.pack(pady=50)


topLabel = Label(root, text="MOM Taker", font=('Helvetica', 24))
topLabel.pack(pady=20)

winButton = ChangingButton(root, text="Pick the Associate", font=("Helvetica", 24))
winButton.pack(pady=20)

resetButton = Button(root, text="Clear", font=("Helvetica", 24))
resetButton.pack(pady=20)

root.mainloop()

Basically, this code creates a custom type of button (ChangingButton) which can do everything a normal button can do but when pressed, changes itself using self.configure(). I also simplified your pick function a little bit, though I don't see why using set() is necessary

I have set the ways it changes itself to the ways which you want, but also note that this custom button now uses this type of change by default, so any other commands which you try to assign to this button will be overwritten. A quick fix to that would be to move self.configure(command=self.pick) to before super().__init__(*args, **kwargs).

Hope I helped!

CodePudding user response:

Your best bet is to use a combobox instead This allows you to change the text in the box from a dropdown. Below is an example.

import os
from tkinter import Label, Tk, ttk


def callbackFunc(event):
    if comboExample.get() == "":
        lbl1["text"] = comboExample.get()   "Nothing was selected"
    else:
        lbl1["text"] = comboExample.get()   " was selected"


root = Tk()


# Add Title from Filename

title = os.path.basename(__file__)[0:-3]
root.title(title.title())
root.geometry("300x120")
labelTop = Label(root, text="Choose your favorite month")
labelTop.grid(column=0, row=0)

lbl1 = Label(root, text="Combo Text")
lbl1.grid(column=0, row=2)

comboExample = ttk.Combobox(
    root,
    values=[
        "",
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ],
)

comboExample.grid(column=0, row=1)
comboExample.current(0)
comboExample.bind("<<ComboboxSelected>>", callbackFunc)

root.mainloop()

If you want to keep using pack here is something I think may be useful

import os
from tkinter import Button, Label, Tk


def rst():
    a.forget()
    b.forget()
    c.forget()
    d.forget()
    e.forget()

    a.pack()
    b.pack()
    c.pack()
    d.pack()
    e.pack()


def clr():
    a.pack_forget()
    b.pack_forget()
    c.pack_forget()
    d.pack_forget()


root = Tk()



root.title('Delete Labels & Buttons')
root.geometry('300x200')
a = Label(root, text='Text to delete')
a.pack()
b = Button(root, text="Delete Text Above", command=lambda: a.pack_forget())
b.pack()
c = Button(root, text="Delete Button above", command=lambda: b.pack_forget())
c.pack()
d = Button(root, text='Delete Everything', command=clr)
d.pack()
e = Button(root, text='Restore', command=rst)
e.pack()
root.mainloop()
  • Related