George Dim
George Dim

Reputation: 85

Text.insert(tk.INSERT ...) Method doesnt work properly in Python

I am trying to display some sentences in "text.insert" method (GUI) of tkinter. The sentences are the results of an another function. I save the indexes of the lsit on another list but when i try to print all the 5 sentences i want it only prints 2 or 3.

I am trying to print them like these : Ans = sent[out[0]] , Ans1=sent[out[1]] .... and i did 5 times , and at the end of the function i wrote :text1.insert(tk.INSERT , Ans,Ans1 ....) As a result it only appears 2 or 3 sentences

import tkinter as tk
from tkinter import *
from collections import Counter
from nltk.tokenize import sent_tokenize, word_tokenize
from tkinter import filedialog
from tkinter.messagebox import *


def my_function():
    global lastScore
    global out
    global sent

    filename = filedialog.askopenfile()
    fileReadyToRead = open(filename.name, 'r')
    file_contents = fileReadyToRead.read()

    data = file_contents

    data = file_contents
    word = word_tokenize(data)
    sent = sent_tokenize(data)

    a = len(sent)
    b = len(word)
    mo = b / a

    number = len(word)

    for x in range(0, number):
        map(str.lower, word[x])

    arraylist = [0] * b
    sentscore = [0] * a

    for x in set(word):
        for i in range(0, a):
            if x in sent[i]:
                sentscore[i] += word.count(x) / mo

    number = len(sent)

    sentList = [0] * number
    listaScore = [0] * number
    lastScore = [0] * number

    for x in range(number):
        sentList[x] = x + 1

    listaScore[0] = listaScore[0] + 0.5
    listaScore[number - 1] = listaScore[number - 1] + 0.5

    for i in range(0, number):
        lastScore[i] = listaScore[i] + sentscore[i]

    out = [i for i in sorted(range(len(lastScore)),
                             key=lastScore.__getitem__, reverse=True)][:5]


def show_answer():

    my_function()
    Ans = "Sentences :  \n",
    Ans0 = out
    Ans00 = out
    Ans1 = sent[out[0]]
    Ans2 = sent[out[1]]
    Ans3 = sent[out[2]]
    Ans4 = sent[out[3]]
    Ans5 = sent[out[4]]

    text1.insert(tk.INSERT, Ans, Ans0, Ans00, Ans1, Ans2, Ans3, Ans4, Ans5)


root = tk.Tk()
root.title("Programm")
text1 = tk.Text(root, heigh=25, width=70)


text1.pack()


button1 = Button(root, text='Result', command=show_answer)
button1.pack()


blank = Entry(root)


def close_window():
    root.destroy()


frame = Frame(root)
frame.pack()


buttonExit = Button(frame, text=" Exit ", command=close_window)
buttonExit.pack()


root.mainloop()

Upvotes: 0

Views: 2529

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386362

The syntax for the insert method is that it takes an index, a string, and then optionally, a list of tags, another string, a list of tags, another string, and so on. The canonical tcl/tk documentation describes it like this:

pathName insert index chars ?tagList chars tagList ...?

...If multiple chars-tagList argument pairs are present, they produce the same effect as if a separate pathName insert widget command had been issued for each pair, in order. The last tagList argument may be omitted.

Given this this statement:

text1.insert(tk.INSERT, Ans, Ans0, Ans00, Ans1, Ans2, Ans3, Ans4, Ans5)

... tkinter will interpret the arguments like this:

  • tk.INSERT - index
  • Ans - text to be inserted
  • Ans0 - one or more tags
  • Ans00 - text to be inserted
  • Ans1 - one or more tags
  • Ans2 - text to be inserted
  • Ans3 - one or more tags
  • Ans4 - text to be inserted
  • Ans5 - one or more tags

If you are intending to insert multiple lines of text, the simplest solution is to join them together with newlines before passing them to the insert method:

data = "\n".join((Ans, Ans0, Ans00, Ans1, Ans2, Ans3, Ans4, Ans5))
text1.insert(tk.INSERT, data)

Upvotes: 1

Related Questions