Python tkinter Entry widget displays bar characters instead of line breaks
\n
characters).
So, I put a Text widget into the GUI and when my script - in example - returns an output which starts with this substring:
RESULT STATUS: OK- Service is currently running\n\nDETAILS: ...
the text displayed contains black vertical bars (|
) whenever there is a \n
line break character.
Lines are correctly broken but those strange bars make me think that the \n
line break character is not correctly decoded, and I don't want the bars in my displayed output.
Any idea on how to make Tkinter display correctly line-endings? Thanks in advance.
Code
This is the workflow for my GUI:
- I click a button, which calls the callMe() callback function
- The callMe() function parses the arguments coming from an Entry widget and then invokes the python command-line script
- The script returns the above-mentioned string and this is used by the callbac开发者_Go百科k to update the text of the Text widget
Here is the code:
#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)
It could be an issue of '\r' characters before each '\n' character (you said you're on Windows).
Before updating the widget, try first:
text_output= text_output.replace('\r', '')
(text_output contains the output of your script, whose contents are to be inserted in the widget)
If you give us more information, we can help you more.
When to use the Entry Widget (from http://effbot.org/tkinterbook/entry.htm)
The entry widget is used to enter text strings. This widget allows the user to enter one line of text, in a single font.
To enter multiple lines of text, use the Text widget.
Without seeing your code it's impossible to say for certain what the problem is. You say you use a text widget but the behavior seems consistent with using an entry widget. Do you still see the vertical bars with the following code?
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()
I solved the question thanks to ΤΖΩΤΖΙΟΥ..it was a matter of /r
characters..I guess that when I invoke the subprocess.Popen to run my command-line script, a Windows Command Prompt is open, the script executed, and the standard output stream is returned by the Prompt with /r/n
line-endings instead of just /n
.
Anyway, I will post the code and GUI workflow all the way...
Workflow
This is the workflow for my GUI:
- I click a button, which calls the callMe() callback function
- The callMe() function parses the arguments coming from an Entry widget and then invokes the python command-line script
- The script returns the above-mentioned string and this is used by the callback to update the text of the Text widget
Code
Here is the code:
#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)
Thanks very much to all of you, guys!
精彩评论