python - 只允许用户在 tkinter 中的文本小部件末尾键入文本

标签 python tkinter text-widget

我想编写一个像 Putty 这样的程序来使用 netmiko 库和 Text tkinter 小部件作为编辑器连接到 Cisco 路由器。

我的问题是如何只允许用户在 Text 的末尾键入命令,而不是在 Text 小部件的其他地方。

比如我的程序连接,发送show ip interface brief,接收输出并显示在编辑器上

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

最佳答案

IDLE 的 Shell,在 idlelib/PyShell.py 中定义,或多或少可以满足您的需求。您可以自由阅读和复制,但代码非常复杂。我自己没有完全阅读和理解它,也不会尝试解释它。

您可能会使用一个带有输入和输出的大型只读文本框和一个小的输入框,比如 3 行,紧靠在主框下方。文本小部件有一个状态。来自 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."

在条目框中,绑定(bind)到移动条目的代码,以及提交它以供操作。请注意,它必须将状态设置回 NORMAL,插入条目,然后再次设置 DISABLED。

关于python - 只允许用户在 tkinter 中的文本小部件末尾键入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37223572/

相关文章:

python - 矩阵程序,使用python中的绑定(bind)来停止/启动

python - Tkinter:如何设置 Canvas 中文本小部件的relwidth?

python - 函数不会突出显示 tkinter 文本小部件中的模式

python - 在 Tkinter 文本小部件中输入时设置文本格式

python - 在 PySpark 上描述数据帧

python - 用热图绘制 2 个变量

python - 带有 next 的异步生成器

python - 返回在 tkinter 列表框中单击的项目的索引

python - 如何设置列表框中项目的颜色

python - 返回值大于但占参数