【问题标题】:Python tkinter Entry widget displays bar characters instead of line breaksPython tkinter Entry 小部件显示条形字符而不是换行符
【发布时间】:2026-02-21 15:20:04
【问题描述】:



我正在编写一个非常简单的 Python Tkinter GUI 来驱动命令行 Python 脚本。
我的 GUI 将在 Windows 主机上运行,​​我希望它显示脚本的多行纯文本输出,该脚本作为包含 - 当然 - 换行符(\n 字符)的字符串返回到 GUI。

所以,我将一个 Text 小部件放入 GUI 中,当我的脚本(例如)返回一个以该子字符串开头的输出时:

RESULT STATUS: OK- Service is currently running\n\nDETAILS: ...

只要有 \n 换行符,显示的文本就会包含黑色竖线 (|)。

线条正确断开,但那些奇怪的条让我认为\n 换行符未正确解码,我不希望在显示的输出中出现这些条。

关于如何使 Tkinter 正确显示行尾的任何想法?提前致谢。

代码

这是我的 GUI 的工作流程:

  1. 我单击了一个按钮,该按钮调用 callMe() 回调函数
  2. callMe() 函数解析来自 Entry 小部件的参数,然后调用 python 命令行脚本
  3. 脚本返回上述字符串,回调使用它来更新 Text 小部件的文本

代码如下:

#init the GUI elements and the Text widget
from Tkinter import *
root = Tk()     
top = Frame(root)
outputFrame = Frame(top)
outputFrame.pack(side='top', padx=20, pady=20)
outputTextArea = Text(outputFrame, yscrollcommand=scrollbar.set, width=150, height=40)
outputTextArea.pack(side='left', expand=YES, fill='both')

#callback executed when the button is clicked
def callMe()

    #get parameters
    # .....


    #command line execution script execution
    process = subprocess.Popen(command_line, stdout=subprocess.PIPE, shell=True)

    #get script output
    matr = process.stdout.readlines()  

    #from a list of strings to a single string
    output = "".join(matr)

    #write output into the Text widget
    outputTextArea.insert(0.0, output)

【问题讨论】:

  • 您说您将 Text 小部件放入 GUI 中,但您描述的行为听起来像是您正在使用 Entry 小部件。您确定您使用的是文本小部件吗?

标签: python user-interface character-encoding tkinter


【解决方案1】:

何时使用 Entry Widget(来自 http://effbot.org/tkinterbook/entry.htm

条目小部件用于输入文本字符串。此小部件允许用户以单一字体输入一行文本。

要输入多行文本,请使用文本小部件。

【讨论】:

  • 按照您的建议,我将 frm Entry 小部件切换为 Text 小部件(这绝对是文本显示的正确小部件选择)。不幸的是,我仍然显示那些烦人的条形图!
【解决方案2】:

如果没有看到您的代码,就无法确定问题所在。您说您使用文本小部件,但行为似乎与使用条目小部件一致。你还看到下面代码的竖线吗?

import Tkinter as tk

OUTPUT = "RESULT STATUS: OK- Service is currently running\n\nDETAILS: ... "

root = tk.Tk()
text = tk.Text(root, height=4, width=80)
text.pack(fill="both", expand="true")
text.insert("end", OUTPUT)

root.mainloop()

【讨论】:

    【解决方案3】:

    这可能是每个 '\n' 字符之前的 '\r' 字符问题(你说你在 Windows 上)。

    在更新小部件之前,请先尝试:

    text_output= text_output.replace('\r', '')
    

    (text_output 包含脚本的输出,其内容将插入到小部件中)

    如果您向我们提供更多信息,我们可以为您提供更多帮助。

    【讨论】:

      【解决方案4】:

      感谢 ΤZΩΤZΙΟΥ,我解决了这个问题..这是/r 字符的问题..我想当我调用 subprocess.Popen 来运行我的命令行脚本时,Windows 命令提示符是打开的,脚本执行后,标准输出流由 Prompt 以/r/n 行结尾返回,而不仅仅是/n

      不管怎样,我会一路贴出代码和GUI工作流程……

      工作流程

      这是我的 GUI 的工作流程:

      1. 我单击了一个按钮,该按钮调用 callMe() 回调函数
      2. callMe() 函数解析来自 Entry 小部件的参数,然后调用 python 命令行脚本
      3. 脚本返回上述字符串,回调使用它来更新 Text 小部件的文本

      代码

      代码如下:

      #init the GUI elements and the Text widget
      from Tkinter import *
      root = Tk()     
      top = Frame(root)
      outputFrame = Frame(top)
      outputFrame.pack(side='top', padx=20, pady=20)
      outputTextArea = Text(outputFrame, yscrollcommand=scrollbar.set, width=150, height=40)
      outputTextArea.pack(side='left', expand=YES, fill='both')
      
      #callback executed when the button is clicked
      def callMe():
      
          #get parameters
          # .....
      
          #command line execution script execution
          process = subprocess.Popen(command_line, stdout=subprocess.PIPE, shell=True)
      
          #get script output
          matr = process.stdout.readlines()  
      
          #from a list of strings to a single string
          output = "".join(matr)  # <<< now it is:  output = "".join(matr).replace('\r', '')
      
          #write output into the Text widget
          outputTextArea.insert(0.0, output)
      



      非常感谢你们,伙计们!

      【讨论】:

        最近更新 更多