F
F
Fizl2021-08-24 18:27:47
Python
Fizl, 2021-08-24 18:27:47

How to deal with invalid command name error?

Hello! This class is like a kind of messenger. Sometimes, when receiving messages from the user with whom the conversation is being conducted, this error occurs:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python\worker.py", line 278, in follow_2
    self.text_show.insert(1.0, l)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 3738, in insert
    self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: invalid command name ".!dialogmenu.!frame.!text"

Here is the code itself
import tkinter as tk
import sqlite3
from tkinter import ttk
from functools import partial
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import codecs
import email
import imaplib
from threading import Thread
import threading
from tkinter import messagebox
import time
from email.header import decode_header
import os

class DIALOGMENU(tk.Toplevel):
    def __init__(self, contact_name):
        try:
            super().__init__(main_root)
            self.color1 = "#808080"
            self.color2 = "#ffff00"
            self.contact_name = contact_name[0:-11]
            self.MAIL = MAIL(f"{self.contact_name}@gmail.com", smtplib.SMTP('smtp.gmail.com:587'))
            self.GET_MAIL = GET_MAIL(imaplib.IMAP4_SSL('imap.gmail.com'))
            self.init_main()
            threading.Thread(target = self.main, daemon = True).start()
        except:
            messagebox.showerror("Ошибка", "Возможно у вас проблемы с интернетом")

    def follow(self, name):
        current = codecs.open(name, "r", encoding = "utf-8")
        curino = os.fstat(current.fileno()).st_ino
        while True:
            while True:
                line = current.readline()
                if not line:
                    break
                yield line
            try:
                if os.stat(name).st_ino != curino:
                    new = open(name, "r")
                    current.close()
                    current = new
                    curino = os.fstat(current.fileno()).st_ino
                    continue
            except IOError:
                pass
            time.sleep(1)

    def follow_2(self):
        if __name__ == '__main__':
            for l in self.follow(f"{self.contact_name}.txt"):
                self.text_show.insert(1.0, l)

    def main(self):
        threading.Thread(target = self.follow_2, daemon = True).start()
        threading.Thread(target = self.GET_MAIL.get_mail_func, daemon=True).start()

    def retrieve_mail(self):
        self.retr_mail = self.text_enteremail.get(1.0, tk.END)
        with open(f"{self.contact_name}.txt", "a+", encoding = "utf-8") as msg_text:
            msg_text.write(f"От Вас\n{self.retr_mail}\n")
        self.MAIL.send_mail(self.retr_mail)

    def init_main(self):
        def color_config(widget, color, event):
            widget.configure(background = color)
        self.title(f'Чат с {self.contact_name}')
        self.geometry('400x550')
        self.resizable(False,False)
        self['bg'] = "#101113"

        self.MAIL.start_client('smtp.gmail.com:587')

        self.btn_dialog_sendmail = tk.Button(self, text = '➤',activebackground = self.color2, padx = 8, pady = 24, font = ('Ubuntu',25), command = self.retrieve_mail)
        self.text_enteremail = tk.Text(self, width = 31, height = 6, bg='white', fg='black', font = ('COMIC SANS MS',10))

        self.btn_dialog_sendmail.bind("<Enter>", partial(color_config, self.btn_dialog_sendmail, self.color2))
        self.btn_dialog_sendmail.bind("<Leave>", partial(color_config, self.btn_dialog_sendmail, self.color1))

        #////SCROLLBAR////
        self.dialog_frame = tk.Frame(self)
        self.text_show = tk.Text(self.dialog_frame,width=26, height=14, font = ("COMIC SANS MS", 15))
        self.scroll = tk.Scrollbar(self.dialog_frame, command=self.text_show.yview)
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.text_show.config(yscrollcommand=self.scroll.set)
        #self.text_show.configure(state='disabled')
        self.text_show.pack(side=tk.TOP) 
        self.dialog_frame.pack(side=tk.TOP)
        #////END SCROLLBAR////

        self.btn_dialog_sendmail.pack()
        self.text_enteremail.pack()

        self.btn_dialog_sendmail.place(x = 290,y = 400)
        self.text_enteremail.place(x = 33, y = 400)

It seems to me that the problem here is in if __name__ == "__main__", but I have not been able to fix this yet.

PS I'll correct the question a little. The error occurs when I close the window when a new message arrives. So yes, the text_show widget is removed. However, after this bug, messages can still come in and be written to text_show, but sometimes duplicated (or even written in 3's)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question