Python Forum
[PyGUI] Invalid command error with Entry object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Invalid command error with Entry object
#1
Hello, I begin in python programmation and i have some problems. I tried to make a handman game with tkinter GUI but I encountered some errors that I don't understand. There are some photos of the code with this post. Can anyone help me pls? thank you

Je parle aussi français

Attached Files

Thumbnail(s)
               
Reply
#2
No images please, post your code.
buran likes this post
Reply
#3
(May-18-2023, 07:14 PM)Axel_Erfurt Wrote: No images please, post your code.

from donnees import *
from tkinter import *
import pickle
from pathlib import Path
from donnees import *
from random import choice
import os


root = Tk()
root.geometry("1080x720")
root.title("Jeu du Pendu")
root.iconbitmap("jeu-du-pendu.ico")


def saisie_utilisateur():
    user_entry = frame.entry2.get()
    frame.entry2.delete(0, END)
    return user_entry


class Interface(Frame):

    def __init__(self, fenetre, **kwargs):
        Frame.__init__(self, fenetre, width=1080, height=720, **kwargs)
        self.pack(expand=YES)

        self.entry1 = Entry(self, bg="#ffe599", width=80, font=("Helvetica", 25))
        self.entry2 = Entry(self, bg="#a2b3d8", width=80, font=("Helvetica", 25))

        self.entry1.pack()
        self.entry2.pack(pady=20)

        self.button = Button(self, text="Entrer", bg="#9999ff", width=20, height=1, fg="white", font=("Helvetica", 20),
                             command=saisie_utilisateur)
        self.button.pack(pady=30)


frame = Interface(root)


def recuperer_scores() -> dict[str, int]:
    chemin_scores = Path(NOM_FICHIER_SCORES)
    if chemin_scores.exists and os.path.getsize(chemin_scores) > 0:
        with chemin_scores.open("rb") as fichier_scores:
            scores = pickle.load(fichier_scores)
    else:
        scores = {}
    return scores


def enregistrer_scores(scores: dict[str, int]):
    chemin_scores = Path(NOM_FICHIER_SCORES)
    with chemin_scores.open("wb") as fichier_scores:
        pickle.dump(scores, fichier_scores)


def enregistrer_nom_utilisateur() -> str:
    frame.entry1.delete(0, END)
    frame.entry1.insert(0, "Saississez votre pseudo dans l'encadré bleu.")
    nom_utilisateur = saisie_utilisateur()
    nom_utilisateur = nom_utilisateur.capitalize()
    if not nom_utilisateur.isalnum():
        frame.entry1.delete(0, END)
        frame.entry1.insert(0, "Le pseudo saisi n'est pas valide.")
        return enregistrer_nom_utilisateur()
    else:
        return nom_utilisateur


def recuperer_lettre() -> str:
    lettre = input("\nSaississez une lettre contenue dans le mot caché:")
    lettre = lettre.lower()
    if not lettre.isalpha() or len(lettre) > 1:
        print("\nVeuillez saisir une lettre valide:")
        return recuperer_lettre()
    else:
        return lettre


def recuperer_mot_masque(mot_complet: str, lettres_trouvees: set[str]) -> str:
    mot_masque = ""
    for lettre in mot_complet:
        if lettre in lettres_trouvees:
            mot_masque += lettre
        else:
            mot_masque += "*"
    return mot_masque


def choisir_mot() -> str:
    mot_choisi = choice(liste_solutions)
    return mot_choisi


root.mainloop()
enregistrer_nom_utilisateur()
error message:

Error:
C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\pythonw.exe "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py" Traceback (most recent call last): File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 97, in <module> enregistrer_nom_utilisateur() File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 59, in enregistrer_nom_utilisateur frame.entry1.delete(0, END) File "C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3105, in delete self.tk.call(self._w, 'delete', first, last) _tkinter.TclError: invalid command name ".!interface.!entry"
deanhystad write May-18-2023, 08:11 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
The reason for your error is because of this:
root.mainloop()
enregistrer_nom_utilisateur()
mainloop() runs until the window is closed. When the window is closed, all the widgets (frames, entries) are deleted. When you call enregistrer_nom_utilisateur(), there is not a frame.entry1 object anymore. When you try to delete the text in frame.entry1 this causes an error.

You can look here to see a tkinter version of hangman.

https://www.codespeedy.com/hangman-game-...g-tkinter/
Reply
#5
(May-18-2023, 08:36 PM)deanhystad Wrote: The reason for your error is because of this:
root.mainloop()
enregistrer_nom_utilisateur()
mainloop() runs until the window is closed. When the window is closed, all the widgets (frames, entries) are deleted. When you call enregistrer_nom_utilisateur(), there is not a frame.entry1 object anymore. When you try to delete the text in frame.entry1 this causes an error.

You can look here to see a tkinter version of hangman.

https://www.codespeedy.com/hangman-game-...g-tkinter/
Reply
#6
thank you but I get this error message after inverting mainloop() and enregistrer_nom_utilisateur() places:

Error:
C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\pythonw.exe "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py" Traceback (most recent call last): File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 96, in <module> enregistrer_nom_utilisateur() File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 993 more times] File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 61, in enregistrer_nom_utilisateur nom_utilisateur = saisie_utilisateur() ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 17, in saisie_utilisateur user_entry = frame.entry2.get() ^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3109, in get return self.tk.call(self._w, 'get') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RecursionError: maximum recursion depth exceeded while calling a Python object Process finished with exit code 1
buran write May-19-2023, 02:37 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#7
That is because your game logic is wrong. enregistrer_nom_utilisateur() should not call itself. It should be called when you press Entrer. Did you check out that link in my previous post. Your code is a mess. It shows that you do not understand how to organize a tkinter application. That link will help you understand how tkinter works, understand the event driven nature of GUI programming which is very different from script programming.
Reply
#8
(May-18-2023, 09:19 PM)deanhystad Wrote: That is because your game logic is wrong. enregistrer_nom_utilisateur() should not call itself. It should be called when you press Entrer. Did you check out that link in my previous post. Your code is a mess. It shows that you do not understand how to organize a tkinter application. That link will help you understand how tkinter works, understand the event driven nature of GUI programming which is very different from script programming.
Reply
#9
ok thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ui to py command error Diovanno 4 20,504 Aug-26-2020, 07:12 AM
Last Post: zorekk
  Database Submit Entry Syntax Error Melford 27 7,712 Jan-27-2020, 04:20 PM
Last Post: Denni
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,463 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,493 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020