- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 252字
- 2021-06-24 18:35:11
Adding the cursor information bar
The cursor information bar is simply a small label at the bottom-right corner of the Text widget that displays the current position of the cursor, as shown in the following screenshot:

The user can choose to show/hide this info bar from the View menu (2.11.py).
Begin by creating a Label widget within the Text widget and pack it in the bottom-right corner, as follows:
cursor_info_bar = Label(content_text, text='Line: 1 | Column: 1')
cursor_info_bar.pack(expand=NO, fill=None, side=RIGHT, anchor='se')
In many ways, this is similar to displaying line numbers. Here, too, the positions must be calculated after every keystroke, after events such as cut, paste, undo, redo, new, and open, or activities that lead to a change in cursor positions. Because this too needs to be updated for all the changed content, for every keystroke, we will update on_content_changed to update this, as follows:
def on_content_changed(event=None):
update_line_numbers()
update_cursor_info_bar()
def show_cursor_info_bar():
show_cursor_info_checked = show_cursor_info.get()
if show_cursor_info_checked:
cursor_info_bar.pack(expand='no', fill=None, side='right', anchor='se')
else:
cursor_info_bar.pack_forget()
def update_cursor_info_bar(event=None):
row, col = content_text.index(INSERT).split('.')
line_num, col_num = str(int(row)), str(int(col)+1) # col starts at 0
infotext = "Line: {0} | Column: {1}".format(line_num, col_num)
cursor_info_bar.config(text=infotext)
The code is simple. We get the row and column for the current cursor position by using the index(INSERT) method and update the labels with the latest row and column of the cursor.
Finally, the function is connected to the existing menu item by using a callback command:
view_menu.add_checkbutton(label='Show Cursor Location at Bottom',
variable=show_cursor_info, command=show_cursor_info_bar)
- Java Web開發學習手冊
- Microsoft Dynamics 365 Extensions Cookbook
- CentOS 7 Server Deployment Cookbook
- C語言程序設計基礎與實驗指導
- 編譯系統透視:圖解編譯原理
- SQL經典實例(第2版)
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- 全棧自動化測試實戰:基于TestNG、HttpClient、Selenium和Appium
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Kotlin開發教程(全2冊)
- Python3.5從零開始學
- Learning Material Design
- Raspberry Pi Robotic Blueprints
- Python機器學習算法與應用
- 零基礎C#學習筆記