Console in Tkinter GUI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 360monkey
    New Member
    • Feb 2010
    • 17

    Console in Tkinter GUI

    Hi Bytes Community!

    I am using tkinter to create a GUI and was wondering how to make a "console" widget embedded in the GUI. Sort of like the python command line but inside the GUI

    Thanks in advance!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Although I have never done it, you could make a "command line console" inside a widget with a Tkinter.Text widget.

    Comment

    • 360monkey
      New Member
      • Feb 2010
      • 17

      #3
      Thanks for the suggestion, but do you know how i can store strings in a list-like format, append strings to the end as I print, and use \n\ in them? a list wont work because it would display as [..., ..., ...]
      any help would be appreciated

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I don't understand what you are trying to accomplish. I made a Tkinter.Text example in combination with an Entry widget. The Entry widget is bound to the Enter key and the Text widget is bound to the Insert key.
        Code:
        import Tkinter
        from itertools import cycle
        
        class App(Tkinter.Tk):
            def __init__(self, textList, master=None):
                Tkinter.Tk.__init__(self, master)
                self.textiter = cycle(textList)
                self.txt = Tkinter.StringVar()
                self.rootEntry = Tkinter.Entry(self, textvariable=self.txt)
                self.rootEntry.pack()
                self.rootEntry.bind("<Return>", self.cycle_text)
                self.rootText = Tkinter.Text(self)
                self.rootText.pack()
                self.rootText.bind("<Insert>", self.insert_all)
                self.newList = []
                
            def cycle_text(self, arg=None):
                t = self.textiter.next()
                self.txt.set(t)
                self.rootText.insert("end", t+"\n")
                self.newList.append(self.rootText.get("end - 2 chars linestart", "end - 1 chars"))
        
            def insert_all(self, arg):
                self.rootText.insert("end", "".join([s.strip() for s in self.newList]))
        
        textList = ["Line 1", "Line 2", "Line 3"]
        root = App(textList)
        root.mainloop()

        Comment

        • 360monkey
          New Member
          • Feb 2010
          • 17

          #5
          Thank You! This is what i was trying to achieve.

          Comment

          • firedoritofinge
            New Member
            • Mar 2020
            • 1

            #6
            How can this be used as a command line, I just get errors like"object has no attribute 'next'", not only that, can this be used as the console for user input, I want to have the user write the user input, in the tk window instead of console, but displaying console in tk window could work to.

            Comment

            Working...