我正在编写一个非常简单的Python Tkinter GUI来驱动命令行python脚本 .
我的GUI将在Windows主机上运行,我希望它显示脚本的多行纯文本输出,该输出作为包含 - 当然 - 换行符( \n 个字符)的字符串返回到GUI .
因此,我将一个Text小部件放入GUI中,当我的脚本(例如)返回以此子字符串开头的输出时:
RESULT STATUS: OK- Service is currently running\n\nDETAILS: ...
只要有 \n 换行符,显示的文本就会包含黑色竖条( | ) .
线被正确打破但是那些奇怪的条让我觉得 \n 换行符没有被正确解码,我不希望显示输出中的条 .
有关如何使Tkinter正确显示行结尾的任何想法?提前致谢 .
代码
这是我的GUI的工作流程:
我点击了一个调用callMe()回调函数的按钮
callMe()函数解析来自Entry小部件的参数,然后调用python命令行脚本
该脚本返回上述字符串,回调函数使用它来更新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)