1

I have a tkinter app in python3 with a Text widget that I insert text into. I would like to append inserted text on the same line as the previous insert, like so:

from tkinter import *

class App :

    def __init__(self):
        sys.stdout.write = self.print_redirect

        self.root = Tk()
        self.root.geometry("900x600")

        self.mainframe = Text(self.root, bg='black', fg='white')
        self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S)) 
        # Set the frame background, font color, and size of the text window
        self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S))

        print( 'Hello: ' )

        print( 'World!' )

    def print_redirect(self, inputStr):
        # add the text to the window widget
        self.mainframe.insert(END, inputStr, None)
        # automtically scroll to the end of the mainframe window
        self.mainframe.see(END)


a = App()
a.root.mainloop()

I would like the resulting insert in the mainframe text widget to look like Hello: World! I'm having a hard time keeping the inserted text on the same line however. Everytime I insert, a new line is generated.

How can I keep the mainframe.insert input string on the same line without a line break?

5
  • Try inserting at a location of "end-1c", rather than END. Commented Jan 27, 2017 at 1:58
  • this code can't run. Please post actual, working code. If you edited the code so that it would actually run, it would indeed result in a text widget that had "Hello: World!", just like you want. Commented Jan 27, 2017 at 2:17
  • @jasonharper: that is not necessary. "end" or END is perfectly acceptable in this case. Commented Jan 27, 2017 at 2:20
  • @BryanOakley you're right, sorry. I was just trying to post the relevant code, but I left out necessary items. I fixed the OP and it correctly illustrates the problem now. (Maybe you could change your downvote now wink wink) Commented Jan 27, 2017 at 2:43
  • 1
    problem is not insert() but print() which always add '\n' at the end - but it is natural. You can use end="" to print text without '\n' - try print( 'Hello: ', end='' ). Or use inputStr.strip('\n') before you insert() text. Commented Jan 27, 2017 at 2:45

1 Answer 1

5

Problem is not insert() but print() which always add '\n' at the end - but it is natural.

You can use end="" to print text without '\n'

print( 'Hello: ', end='' ) 

or directly

sys.stdout.write( 'Hello: ' )

Or in insert() use

inputStr.strip('\n')

But it will remove all '\n' - even if you will need '\n' ie.

print( 'Hello:\n\n\n' ) 

You will never know if you have to remove last '\n' or not.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I needed! I've already tested it an it solves my problem. Thanks for working through it with me.
BTW: you can also use sys.stdout.write() instead of print() and then you don't have problem with '\n' and with spaces which ar added by print when you use comma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.