This library gives Terminal widget support to the Tkinter library. Perform almost all the operations of a terminal with tkterminal.
Use the package manager pip to install with the following command:
pip install tkterminal
If you would like to get the latest master or branch from GitHub, you could also:
pip install git+https://github.com/Saadmairaj/tkterminal
Or even select a specific revision (branch/tag/commit):
pip install git+https://github.com/Saadmairaj/tkterminal@master
Terminal widget is easy to use. Type the commands just like you type in the terminal.
import tkinter as tk
from tkterminal import Terminal
root = tk.Tk()
terminal = Terminal(pady=5, padx=5)
terminal.pack(expand=True, fill='both')
root.mainloop()
Enable shell (shell=True
)
If the shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory. However, note that Python itself offers implementations of many shell-like features (in particular, glob
, fnmatch
, os.walk()
, os.path.expandvars()
, os.path.expanduser()
, and shutil
).
To have shell enabled terminal then set the shell to true like so
terminal = Terminal()
terminal.shell = True
Enable line number bar for the terminal (linebar=True
)
If linebar is True, the terminal will have a line number bar on the left side which tells the number of the line by numbering each line of the terminal. Clicking on any number will select the specific line in the terminal. The line with the insert will be highlighted in the number bar.
To have line number bar enabled terminal then set linebar to true like so.
terminal = Terminal()
terminal.linebar = True
Command that requires input.
The tkterminal is using subprocess python module where the input can only be passed before running the command and cannot be passed after the command has ran. So the input can be pass with methods:
run_command(cmd, give_input=None)
_ method.Input can be passed directly to the parameter give_input
of run_command method along with the actual command.
Example:
import tkinter as tk
from tkterminal import Terminal
root = tk.Tk()
terminal = Terminal(pady=5, padx=5)
terminal.shell = True
terminal.linebar = True
terminal.pack(expand=True, fill='both')
b1 = tk.Button(
root, text="Uninstall tkterminal", fg="Black",
command=lambda: terminal.run_command('pip uninstall tkterminal', 'y'))
b1.pack()
root.mainloop()
We can directly pass input into the terminal after typing the command between these HTML tags <input> ... </input>
these tags are just to read user input from the command.
Example:
pip uninstall tkterminal <input>y</input>
Terminal widget is created from the Tkinter Text widget class that makes it support all the options of a Text widget.
Configurable options for a Terminal widget. Syntax: Terminal(root, options=value, ...)
Options | Description |
---|---|
background | The default background color of the terminal widget. |
borderwidth | The width of the border around the terminal widget. Default is 2 pixels. |
cursor | The cursor that will appear when the mouse is over the terminal widget. |
exportselection | Normally, text selected within a terminal widget is exported to be the selection in the window manager. Set exportselection=0 if you don't want that behavior. |
font | The default font for text inserted into the widget. |
foreground | The color used for text within the widget. You can change the color for tagged regions; this option is just the default. |
height | The height of the widget in lines (not pixels!), measured according to the current font size. |
highlightbackground | The color of the focus highlight when the terminal widget does not have focus. |
highlightcolor | The color of the focus highlight when the terminal widget has the focus. |
highlightthickness | The thickness of the focus highlight. Default is 0. |
insertbackground | The color of the insertion cursor. Default is black. |
insertborderwidth | Size of the 3-D border around the insertion cursor. Default is 0. |
insertofftime | The number of milliseconds the insertion cursor is off during its blink cycle. Set this option to zero to suppress blinking. Default is 300. |
insertontime | The number of milliseconds the insertion cursor is on during its blink cycle. Default is 600. |
insertwidth | Width of the insertion cursor (its height is determined by the tallest item in its line). Default is 2 pixels. |
padx | The size of the internal padding added to the left and right of the text area. Default is one pixel. |
pady | The size of the internal padding added above and below the text area. Default is one pixel. |
relief | The 3-D appearance of the terminal widget. Default is relief=SUNKEN . |
selectbackground | The background color to use displaying selected text. |
selectborderwidth | The width of the border to use around selected text. |
spacing1 | This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0. |
spacing2 | This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0. |
spacing3 | This option specifies how much extra vertical space is added below each line of text. If a line wraps, this space is added only after the last line it occupies on the display. Default is 0. |
state | Normally, terminal widgets respond to keyboard and mouse events; set state=NORMAL to get this behavior. If you set state=DISABLED, the terminal widget will not respond, and you won't be able to pass commands into the terminal. |
tabs | This option controls how tab characters position text. |
width | The width of the widget in characters (not pixels!), measured according to the current font size. |
wrap | This option controls the display of lines that are too wide. Set wrap=WORD and it will break the line after the last word that will fit. With the default behavior, wrap=CHAR , any line that gets too long will be broken at any character. |
xscrollcommand | To make the terminal widget horizontally scrollable, set this option to the set() method of the horizontal scrollbar. |
yscrollcommand | To make the terminal widget vertically scrollable, set this option to the set() method of the vertical scrollbar. |
Methods on Terminal
widget objects:
Methods | Description |
---|---|
.clear() | Clears the console completely. |
_.getoutput() | Get the output of the recently run command. Returns None if no command has run else the function will return a dictionary of error and output. |
_.run_command(cmd, giveinput=None) | Run the command into the terminal. |
_.tagconfig(tagname, option=value...) | You can use this method to configure the tag properties, which include:- - background - foreground - font - justify (center, left, or right), - tabs (this property has the same functionality of the Text widget tabs's property) - underline (used to underline the tagged text). |
Properties on Terminal
widget objects:
Properties | Description |
---|---|
terminal.basename | Change the basename of the terminal. Default is tkterminal$ |
terminal.linebar | Line number bar tells the number of the line by numbering each line of the terminal. Clicking on any number will select the specific line in the terminal. The line with the insert will be highlighted in the number bar. Default is False. |
Configure output, error, basename and linebar:
terminal.tag_config("output", option=value...)
method.terminal.tag_config("error", option=value...)
method.terminal.tag_config("basename", option=value...)
method.terminal.linebar.configure(option=value ...)
. And each number line is a canvas item whose tag name is the number itself that can be configured with terminal.linebar.itemconfigure('item', options...)
.