【问题标题】:How to print a specific line from tkinter multiline Text widget如何从 tkinter 多行文本小部件打印特定行
【发布时间】:2016-12-16 21:08:32
【问题描述】:

我在 tkinter 多行文本小部件中编写了此文本。

他是个男孩。

马上就要下雨了

今天是星期天。

.

如果我输入例如行号。 2 它应该打印第二行“马上要下雨了”

如何找到行尾?

【问题讨论】:

    标签: python-2.7 python-3.x text tkinter widget


    【解决方案1】:

    您可以将修饰符应用于索引。因此,例如,要获得第三行的结尾,您可以使用索引"3.0 lineend"。例如:

    the_text.get("3.0", "3.0 lineend")
    

    对索引及其修饰符的最终参考位于此处的 tcl/tk 文档中:http://tcl.tk/man/tcl8.5/TkCmd/text.htm#M7

    【讨论】:

      【解决方案2】:

      您还可以将整个文本保存为字符串,然后在每个换行符处拆分字符串。然后只需选择您需要的任何一行。

      lines = 'He is a boy. \n It will rain soon \n Today is Sunday.'
      line_list = lines.split('\n')
      print line_list[0]
      print line_list[1]
      print line_list[2]
      
      >>>>He is a boy.
      >>>>It will rain soon
      >>>>Today is Sunday.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        相关资源
        最近更新 更多