Python tkinter Entry小部件显示条形字符而非换行符

2 投票
4 回答
3124 浏览
提问于 2025-04-16 19:01



我正在编写一个非常简单的Python Tkinter图形界面,用来运行一个命令行的Python脚本。
我的这个图形界面会在Windows系统上运行,我希望它能显示脚本的多行纯文本输出,这些输出会作为字符串返回给图形界面,当然,这里面会包含换行符(\n字符)。

所以,我在图形界面里放了一个文本框,当我的脚本返回的输出以以下内容开头时:

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

文本框里显示的内容会在每个\n换行符的地方出现黑色的竖线(|)。

虽然行是正确换行的,但那些奇怪的竖线让我觉得\n换行符没有被正确解析,我不想在显示的输出中看到这些竖线。

有没有什么办法可以让Tkinter正确显示换行呢?提前谢谢你。

代码

这是我图形界面的工作流程:

  1. 我点击一个按钮,这会调用callMe()这个回调函数
  2. callMe()函数解析来自输入框的参数,然后调用Python命令行脚本
  3. 脚本返回上面提到的字符串,回调函数用这个字符串来更新文本框的内容

以下是代码:

#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)

4 个回答

0

在没有看到你的代码之前,确实很难确定问题出在哪里。你提到你使用了一个文本小部件,但它的表现看起来更像是使用了一个输入框。你用下面的代码还会看到竖条吗?

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()
0

我解决了这个问题,得益于ΤΖΩΤΖΙΟΥ的帮助。其实就是个关于 /r 字符的问题。我猜当我用 subprocess.Popen 来运行我的命令行脚本时,Windows 命令提示符会打开,脚本执行后,提示符会用 /r/n 的换行符返回标准输出,而不是简单的 /n



无论如何,我会把代码和图形界面的工作流程全部发出来...



工作流程

这是我图形界面的工作流程:

  1. 我点击一个按钮,这个按钮会调用 callMe() 回调函数
  2. callMe() 函数会解析来自一个输入框(Entry widget)的参数,然后调用 Python 命令行脚本
  3. 脚本返回上面提到的字符串,回调函数用这个字符串来更新文本框(Text widget)的内容

代码

以下是代码:

#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)



非常感谢你们,伙计们!

3

这可能是因为在每个换行符('\n')前面有一个'\r'字符(你提到你是在Windows系统上)。

在更新这个小部件之前,先试试这个:

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

(text_output是你脚本的输出内容,这些内容会被插入到小部件中)

如果你能提供更多信息,我们可以给你更多帮助。

撰写回答