2

I want to write a program like Putty to connect to Cisco router using netmiko library and Text tkinter widget as the editor.

My question is how to only allow user to type command at the end of Text but not elsewhere on the Text widget.

For example, my program connect, send show ip interface brief, receive the output and display it on the editor

Router# show ip interface brief
Interface     IP-Address     OK?  Method  Status                  Protocol
Ethernet0     10.108.00.5    YES  NVRAM   up                      up      
Ethernet1     unassigned     YES  unset   administratively down   down    
Loopback0     10.108.200.5   YES  NVRAM   up                      up
Router# <-- user can only type command from here

The program look like this

vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • will the user ever have to type more than one line at a time, or will the one line be so long that it will wrap to a second line? – Bryan Oakley May 14 '16 at 11:14
  • The user is only type one line at a time but this line may be so long that it will wrap to a second line. – Hoàn Bùi Quốc May 14 '16 at 12:31
  • I've edited my post to provide an image describe the program will look like. The lines mark in red are commands that user type in to the program. After the user press Enter key, the program will send the command to device that it connects to, wait for device return output and display it on the screen (lines mark in yellow). – Hoàn Bùi Quốc May 14 '16 at 16:26
  • Question [How can I create a small IDLE-like Python Shell in Tkinter?](https://stackoverflow.com/questions/59164314/how-can-i-create-a-small-idle-like-python-shell-in-tkinter) may help. – acw1668 Feb 11 '22 at 08:45

1 Answers1

-1

IDLE's Shell, defined in idlelib/PyShell.py, does more or less what you want. You are free to read and copy, but the code is pretty complex. I have not completely read and understood it myself, and will not try to explain it.

You might instead go with a large read-only text box with inputs and outputs and a small entry box, of say 3 lines, set immediately below the main box. Text widgets have a state. From this reference

"Normally, text widgets respond to keyboard and mouse events; set state=tk.NORMAL to get this behavior. If you set state=tk.DISABLED, the text widget will not respond, and you won't be able to modify its contents programmatically either."

In the entry box, bind to code that moves the entry, as well as submitting it for action. Note that it must set the state back to NORMAL, insert the entry, and set DISABLED again.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52