如何删除按钮单击上的输入文本并在tkinter中格式化输出文本?

时间:2014-06-14 14:11:35

标签: python tkinter

我有以下代码:

# -*- coding: utf-8 -*-

forbiddenWords=['for', 'and', 'nor', 'but', 'or', 'yet', 'so', 'not', 'a', 'the', 'an', 'of', 'in', 'to', 'for', 'with', 'on', 'at', 'from', 'by', 'about', 'as']

def new_sentences(sentenceList):
    global newsentences
    newsentences=[]
    for lause in sentenceList:
        import re
        from random import randint
        s6nade_arv=len(lause.split(' '))
        while True:
            asendatava_idx=randint(0,s6nade_arv-1)
            wordList = re.sub("[^\w]", " ",  lause).split()
            asendatav_s6na=wordList[asendatava_idx]
            if asendatav_s6na.lower() not in forbiddenWords:
                break
        uus_lause=lause.replace(asendatav_s6na, "______")
        newsentences.append(uus_lause)
    return newsentences

def IntoSentences(paragraph):
    paragraph = paragraph.decode('utf-8').replace(u'\u014c\u0106\u014d','-')
    import nltk.data
    sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
    global sentenceList
    sentenceList = sent_detector.tokenize(paragraph.strip())
    return sentenceList

def full_function():
    IntoSentences(text.get(1.0,END))
    new_sentences(sentenceList)
    text.insert(INSERT, newsentences)

from Tkinter import *

root = Tk()

var = StringVar()
label = Label( root, textvariable=var)
var.set("Fill in the caps: ")
label.pack()

text = Text(root)
text.pack()

button=Button(root, text ="Create text with caps.", command =lambda: full_function())
button.pack()

root.mainloop()

一切都按照必要的方式运作。该程序将用户输入作为一些句子。单击该按钮时,它应输出带有大写字母的文本。但是我想改变两件事。首先在每个句子的开头都有字母“u”,如何删除?其次如何使程序从屏幕上删除输入文本,它输出带有大写字母的文本?

1 个答案:

答案 0 :(得分:0)

你是unicode,你只需要在某个地方拨打一个字符串。最后尝试text.insert(INSERT, str(new sentences))。至于摆脱旧句子,我相信Tkinter这样做的方法就是在插入之前删除它。在插入之前,请尝试将text.delete(1.0, END)添加到主要功能中。