Home > Software engineering >  Entry Tkinter doesn't appear at the launching of the script
Entry Tkinter doesn't appear at the launching of the script

Time:04-04

I created a little gui with tkinter to train myself to tkinter. There is an entry in my script, but when I launch the script the entry doesn't appear, I have to click on it to make it appear. Does anyone has a solution to make it appear directly ? Thanks

Picture of the gui with the problem here

from tkinter import *

app = Tk()
app.title("Vérification palindrome")
app.geometry("320x100")
app.resizable(width=False, height=False)

txt = Entry(app) #entrée utilisateur
ch = Label(app) #label qui affiche le resultat, vide/invisible au demarrage

txt.grid(row=0, column=0, sticky=W)
ch.grid(row=1, column=0)

#fonction de verification du palindrome
def palindrome():
    ch.grid(row=1, column=0) #permet de réafficher un resultat si la fonction de nettoyage d'écran a été utilisée
    mot = txt.get()
    mot = mot.lower()
    if mot == "":
        reponse = "Veuillez indiquez un mot ou un nombre"
        ch.configure(text = reponse)
    else:
        motinverse = ''.join(reversed(mot))
        if mot == motinverse:
            reponse = mot   " est un palindrome"
            ch.configure(text = reponse)
        else:
            reponse = mot   " n'est pas un palindrome"
            ch.configure(text = reponse)

Button(app,text='Vérifier',command=palindrome).grid(row=0 , column=1) # bouton qui lance la commande

#fonction de "nettoyage" de l'ecran
def clear():
    ch.grid_forget()
    txt.delete("0","end")

Button(app,text='Vider',command=clear).grid(row=1 , column=1) # bouton qui lance la commande
app.mainloop()

CodePudding user response:

You can use txt.focus_force() to write in it without the need of clicking on it first.

from tkinter import *

app = Tk()
app.title("Vérification palindrome")
app.geometry("320x100")
app.resizable(width = False, height = False)

txt = Entry(app)
ch = Label(app)

txt.grid(row = 0, column = 0, sticky = W)

txt.focus_force() #whenever you want to do so, use this line of code

ch.grid(row = 1, column = 0)
  • Related