如何创建tkinter文本输入数组,其中text中的每一行都是数组中的元素

时间:2014-06-23 09:58:54

标签: python tkinter

我有这段代码:

def full_function():
    options=text.get(1.0, END)
    print options

from Tkinter import *

root = Tk()

text = Text(root,font=("Purisa",12))
text.pack()

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

root.mainloop()

我怎样才能使这个程序创建一个文本输入数组,因此每行文本都是数组中的元素。例如,如果输入为:

                                              I am here.

                                              They are there.

然后我希望我的应用创建以下list = ["I am here.", "They are there."]

2 个答案:

答案 0 :(得分:0)

使用split制作输入列表,并使用str(text.get(1.0, END))删除unicode "u"

def full_function():
    options = str(text.get(1.0, END))
    lines = options.strip().split("\n")
    print lines
['I am here.', 'They are there.']

答案 1 :(得分:0)

from Tkinter import *

#Below should pack to class
class MyWindow:
    def __init__(self, wname=""):
        self.root = Tk()
        self.root.title(wname)
        self.text = Text(self.root,font=("Purisa",12))
        self.text.pack()

        self.button=Button(self.root, text ="Button", command=self.prn)
        self.button.pack()
        self.root.mainloop()

    def get_words_list(self, widget):
        """Return list of words"""
        options=widget.get(1.0, END)# get lines into string
        #Remove spaces in list of lines
        return [i.strip() for i in options.splitlines()]

    def prn(self):
        """Testing get_words_list"""
        print self.get_words_list(self.text)

if __name__ == "__main__":
    w = MyWindow("My window")