By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,465 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,465 IT Pros & Developers. It's quick & easy.

Console in Tkinter GUI

360monkey
P: 17
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!
Feb 22 '10 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Although I have never done it, you could make a "command line console" inside a widget with a Tkinter.Text widget.
Feb 22 '10 #2

360monkey
P: 17
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
Feb 22 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. from itertools import cycle
  3.  
  4. class App(Tkinter.Tk):
  5.     def __init__(self, textList, master=None):
  6.         Tkinter.Tk.__init__(self, master)
  7.         self.textiter = cycle(textList)
  8.         self.txt = Tkinter.StringVar()
  9.         self.rootEntry = Tkinter.Entry(self, textvariable=self.txt)
  10.         self.rootEntry.pack()
  11.         self.rootEntry.bind("<Return>", self.cycle_text)
  12.         self.rootText = Tkinter.Text(self)
  13.         self.rootText.pack()
  14.         self.rootText.bind("<Insert>", self.insert_all)
  15.         self.newList = []
  16.  
  17.     def cycle_text(self, arg=None):
  18.         t = self.textiter.next()
  19.         self.txt.set(t)
  20.         self.rootText.insert("end", t+"\n")
  21.         self.newList.append(self.rootText.get("end - 2 chars linestart", "end - 1 chars"))
  22.  
  23.     def insert_all(self, arg):
  24.         self.rootText.insert("end", "".join([s.strip() for s in self.newList]))
  25.  
  26. textList = ["Line 1", "Line 2", "Line 3"]
  27. root = App(textList)
  28. root.mainloop()
Feb 22 '10 #4

360monkey
P: 17
Thank You! This is what i was trying to achieve.
Feb 25 '10 #5

P: 1
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.
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.