【问题标题】:How to show line number and column number in status bar in tkinter python如何在tkinter python的状态栏中显示行号和列号
【发布时间】:2021-11-12 12:37:24
【问题描述】:

我想在状态栏中添加行号和列号,但我不明白如何添加。请帮助我,如何在 Python Tkinter 状态栏中显示行号和列号。

from tkinter import *

root = Tk()
root.title("Testing 6.py")
root.geometry('500x500')

StatusBar = Frame(root, width=10, borderwidth=1, relief=SUNKEN)
StatusBar.pack(side=BOTTOM, fill=X)
StatusBarlabel = Label(StatusBar, text='My Notepad')
StatusBarlabel.pack(side=LEFT)


root.mainloop()

【问题讨论】:

    标签: python tkinter statusbar


    【解决方案1】:

    据我了解,您希望显示光标当前在文本小部件中的位置(行:列),所以这里有一个关于如何做到这一点的最小示例:

    from tkinter import Tk, Text, Label
    
    
    def update_label():
        row, col = text.index('insert').split('.')
        label.config(text=f'{row}:{col}')
        root.after(100, update_label)
    
    
    root = Tk()
    
    text = Text(root)
    text.pack(fill='both', expand=True)
    
    label = Label(root, anchor='e')
    label.pack(fill='x')
    update_label()
    
    root.mainloop()
    

    基本上只是有一个循环,它将使用.index 方法和"insert" 的值不断获取当前索引,它返回当前光标所在位置的索引,然后循环并配置标签的文本以显示这一点给用户。

    来源:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 2022-11-23
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多