Skip to main content

Terminal widget for Tkinter library.

Project description

tkterminal

CodeFactor FOSSA Status Platform

This library gives Terminal widget support to the Tkinter library. Perform almost all the operations of a terminal with tkterminal.

demo_dark demo_light

Installation

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

Usage

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
    

    linebar

  • 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:

    • Through 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()
      
    • Directly typing into the Terminal window.

      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>
      

Documentation

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.
    .get_output() 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, give_input=None) Run the command into the terminal.
    .tag_config(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:

    • All output text have tag name output which can be configured with terminal.tag_config("output", option=value...) method.
    • All error text also have a tag name error which can be configured with terminal.tag_config("error", option=value...) method.
    • The basename also have a tag name basename configured with terminal.tag_config("basename", option=value...) method.
    • The linebar is an object of Tkinter Canvas widget which can be configured with 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...).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkterminal-0.1.0b1.tar.gz (18.5 kB view hashes)

Uploaded Source

Built Distribution

tkterminal-0.1.0b1-py3-none-any.whl (16.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page