文本.插入(tk.插入…)方法在Python中无法正常工作

2024-10-02 14:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在“中显示一些句子”文本.插入tkinter的方法(GUI)。 这些句子是另一个函数的结果。 我将lsit的索引保存在另一个列表中,但是当我试图打印所有5个句子时,我只想打印2或3个句子。你知道吗

我试着这样打印它们:Ans=sent[out[0]],Ans1=sent[out[1]]。。。。 我做了5次,在函数的末尾我写了:text1.insert(tk.插入,答案,答案1…) 结果它只出现了2到3句话

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()

Tags: infromimportnumberforlentkinterroot
1条回答
网友
1楼 · 发布于 2024-10-02 14:27:52

insert方法的语法是,它获取一个索引、一个字符串,然后可选地获取一个标记列表、另一个字符串、一个标记列表、另一个字符串等等。规范的tcl/tk文档描述如下:

pathName insertindex 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.

鉴于此声明:

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

。。。tkinter会这样解释这些论点:

  • 你知道吗tk.插入-索引
  • Ans-要插入的文本
  • Ans0-一个或多个标记
  • Ans00-要插入的文本
  • Ans1-一个或多个标记
  • Ans2-要插入的文本
  • Ans3-一个或多个标签
  • Ans4-要插入的文本
  • Ans5-一个或多个标签

如果要插入多行文本,最简单的解决方案是在将它们传递给insert方法之前用换行符将它们连接在一起:

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

相关问题 更多 >