pythonide显示行号_python,Tkinter中控件ScrolledText,如何显示行号?

显示行号有两种意思:在每行前显示一个阿拉伯数字行号,类似其他IDE。这种直接计算出 ScrolledText 控件的行高,再用一个 Label 控件写上数字放在左边布局即可。

显示当前光标的行号,这里需要用到 tk.CURRENT 标记,Python3 代码如下。以下代码显示了当前光标位置的行号和行内字符偏移。

import tkinter as tk

from tkinter import scrolledtext, messagebox

win = tk.Tk()

scr = scrolledtext.ScrolledText(win, width=60, height=20, wrap=tk.WORD)

scr.pack()

def on_click():

messagebox.showinfo(title=None, message=scr.index(tk.CURRENT))

btn = tk.Button(win, text="Click", command=on_click)

btn.pack()

win.mainloop()

由于 Python 内置的 tkinter 只是 tcl/tk 的一层面向对象包装,所以,主要需要查询的文档都在TclTk中。

上述代码参考的方法和常量来自:https://www.tcl.tk/man/tcl8.6/TkCmd/text.htm​www.tcl.tkTwo marks have special significance. First, the mark insertis associated with the insertion cursor, as described under currentis associated with the character closest to the mouse and is adjusted automatically to track the mouse position and any changes to the text in the widget (one exception: currentis not updated in response to mouse motions if a mouse button is down; the update will be deferred until all mouse buttons have been released). Neither of these special marks may be deleted. With the exception of these two special marks, all marks are shared between peer text widgets, and may be manipulated on an equal basis from any peer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值