如何从 Python 中的多行 Tkinter 文本框读取输入(逐行)? [英] How to read the input(line by line) from a multiline Tkinter Textbox in Python?

查看:44
本文介绍了如何从 Python 中的多行 Tkinter 文本框读取输入(逐行)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在 Python 中使用过程编程范式,我编写了一个程序,该程序逐行读取文件(比如 File.txt)的输入并打印它.下面是示例:

By Using Procedural Programming Paradigm in Python, I have a written a program which reads the input of a file(say File.txt) line by line and print it. Below is the Example:

脚本:

import fileinput
for line in fileinput.input(r'D:\File.txt'):
    line = line.replace(" ", "")
    line = line[0:-1]
    print(line)

结果:

注意:例如File.txt"包含两行,第一行为'line1',第二行为'line2',则输出为:

Note: If for example "File.txt" contains 2 lines, First line as 'line1' and Second line as 'line2', then output is:

line1
line2

通过使用Tkinter Multiline TextBox"而不是文件(在上面的示例 File.txt 中),我希望通过面向对象的编程范式获得相同的结果.

Same Result I want through Object Oriented programing Paradigm by using "Tkinter Multiline TextBox" instead of a file(here in above example File.txt).

我有下面的代码来创建一个多行 Tkinter 文本框:

I have the Below Code that creates a MultiLine Tkinter TextBox:

import tkinter as tki # Tkinter -> tkinter in Python3
class App(object):

    def __init__(self):
        self.root = tki.Tk()

    # create a Frame for the Text and Scrollbar
        txt_frm = tki.Frame(self.root, width=200, height=200)
        txt_frm.pack(fill="both", expand=True)

        # ensure a consistent GUI size
        txt_frm.grid_propagate(False)

        # implement stretchability
        txt_frm.grid_rowconfigure(0, weight=1)
        txt_frm.grid_columnconfigure(0, weight=1)

    # create a Text widget
        self.txt = tki.Text(txt_frm, borderwidth=3, relief="sunken")
        self.txt.config(font=("consolas", 12), undo=True, wrap='word')
        self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

    # create a Scrollbar and associate it with txt
        scrollb = tki.Scrollbar(txt_frm, command=self.txt.yview)
        scrollb.grid(row=0, column=1, sticky='nsew')
        self.txt['yscrollcommand'] = scrollb.set

    def retrieve_input():
        input = self.txt.get("0.0",END)
        print(input)
app = App()
app.root.mainloop()
app.retrieve_input()

现在我的问题是,一旦我运行上面的代码,就会出现一个Tkinter Multiline TextBox",我在 Tkinter TextBox 中输入 4 行:

Now My Problem is, once I run the above code a "Tkinter Multiline TextBox" is coming and I am entering 4 Lines in the Tkinter TextBox as:

ABC
XYZ
PQR
QAZ

但我没有得到确切的想法/实现,即如何从 Tkinter TextBox 中一一读取这些行并在我的程序中使用它们进行进一步处理.

But I am not getting the Exact Idea/Implementation that How I can read those lines form Tkinter TextBox one by one and Use them for further processing in My Program.

我使用的 Python 版本是 3.0.1.请帮助...

Python Version I am using is 3.0.1. Please Help...

推荐答案

从文档来看,tkinter.text 上的 get 方法只会返回字符串,包括新行 \n.您不能将 tkinter.text 视为文件,但您可以使用其他方式.

From the document, the get method on tkinter.text will just return the string, including the new line \n. You can not treat the tkinter.text as file, but you can use other ways.

  1. 阅读它们并将它们分成一个列表.然后循环列表.

  1. Read them all and split them into a list. Loop the list then.

def retrieve_input():
    text = self.txt.get('1.0', END).splitlines()
    for line in text:
        ...

  • 使用 io.StringIO 来模拟一个文件,但在这种情况下它不会去除换行符.

  • Using io.StringIO to emulate a file, but in this case it will not strip the newline.

    def retrieve_input():
        text = io.StringIO(self.txt.get('1.0', END))
        for line in text:
            line = line.rstrip()
            ...
    

  • 这篇关于如何从 Python 中的多行 Tkinter 文本框读取输入(逐行)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆