It is necessary to insert text from files into Entry and Text , but tkinter behaves very badly at a bad index. I tried all the options:

"0.1", "1.0", 0, 1, 1.0, 0.1, 0.0

Nothing works. True, the error occurs exactly when trying to fill in the text of the Entry , and with the same operations with Text the program works fine. Here is a part of the program:

 import tkinter, realize_Grons import os def open_test(): """Функция проверки файла на пустоту, для восстановления раннее введеных данных(Текст, Ключ)""" # для проверки файла на пустоту, используем библиотеку os print("Yes_01") if os.stat("keys_here").st_size != 0 and os.stat("text_here").st_size != 0: print("Yes_02") with open("keys_here", mode="r", encoding="latin_1") as f: key.insert(index="1.0", string=f.read()) with open("text_here", mode="r", encoding="latin_1") as f: input_text.insert(index="1.0", chars=f.read()) elif os.stat("keys_here").st_size != 0: print("Yes_03") with open("keys_here", mode="r", encoding="latin_1") as f: key.insert(index="1.0", string=f.read()) elif os.stat("text_here").st_size != 0: print("Yes_04") with open("text_here", mode="r", encoding="latin_1") as f: input_text.insert(index="1.0", chars=f.read()) root = tkinter.Tk() root.title("Шифр Гронсфельда") frame_1 = tkinter.Frame(root, bg="white") frame_2 = tkinter.Frame(root, bg="white") label_to_input_text = tkinter.Label(frame_1, text="Введите текст: ") label_to_output_text = tkinter.Label(frame_1, text="Зашифрованный текст: ") input_text = tkinter.Text(frame_1, width=40, height=20, bg="white") output_text = tkinter.Text(frame_1, width=40, height=20, bg="white") output_text.config(state="disabled") # перенос слова в окне ввода текста input_text.config(wrap="word") label = tkinter.Label(frame_2, text="Введите Ключ смещения: ", width=20, bg="white") key = tkinter.Entry(frame_2) encrypt_it = tkinter.Button(frame_2, text="Зашифровать!") # инилциализируем переменные для checkbox var_1 = tkinter.IntVar() var_2 = tkinter.IntVar() # описание checkbox user_choice_1 = tkinter.Checkbutton(frame_2, text="Сохранить Ключ", variable=var_1) user_choice_2 = tkinter.Checkbutton(frame_2, text="Сохранить Текст", variable=var_2) # Настройка frame_1 and frame_2 frame_1.grid(row=0, column=0) frame_2.grid(row=0, column=1) # Настройка объектов frame_1 label_to_input_text.pack() input_text.pack() label_to_output_text.pack() output_text.pack() # Настройка объектов frame_2 label.grid(row=0, column=0) key.grid(row=1, column=0) encrypt_it.grid(row=2, column=0) user_choice_1.grid(row=3, column=0, sticky="w") user_choice_2.grid(row=4, column=0, sticky="w") open_test() # обработка событий encrypt_it.bind("<Button-1>", crupte_it) # обработка выхода из приложения root.protocol("WM_DELETE_WINDOW", on_exit) root.mainloop() 
  • one
    index must be an integer or a string containing an integer and indicates the position of the insertion of the string - godva
  • @godva. Replaced all Entry with index 0 and earned. Thank you, the truth is very strange that for the Text the type float worked, I suspect this is due to the fact that in the case of Text the view goes in the form of a table, because there are a lot of rows and the type is float, and Entry has 1 line and type int, but it is not accurate. - helsereet

1 answer 1

Entry widget

As noted above:

index must be an integer, or a string containing an integer, and indicates the position of the insert row

The tkinter entry widget

Text widget

In a Text widget, the index is represented by a string with two numbers separated by a dot:

 "%d.%d" % (line, column) 

In this case, the row numbers begin with 1, the column numbers - with 0.

The tkinter text widget