Skip to Content
Tkinter GUI Programming by Example
book

Tkinter GUI Programming by Example

by David Love
April 2018
Beginner content levelBeginner
340 pages
7h 54m
English
Packt Publishing
Content preview from Tkinter GUI Programming by Example

Getting the cursor's position

Open up a new Python file and enter the following code:

import tkinter as tkwin = tk.Tk()current_index = tk.StringVar()text = tk.Text(win, bg="white", fg="black")lab = tk.Label(win, textvar=current_index)

Begin with the normal importing and creation of a main window.

The things we will need for this application are a StringVar to hold the current cursor location, a Text widget to navigate around, and a Label to display our StringVar.

Now we need a function to hook to the <KeyRelease> event which will update our StringVar with the current cursor coordinates:

def update_index(event=None):    cursor_position = text.index(tk.INSERT)    cursor_position_pieces = str(cursor_position).split('.')    cursor_line = cursor_position_pieces ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Tkinter GUI Application Development Blueprints, Second Edition - Second Edition

Bhaskar Chaudhary

Publisher Resources

ISBN: 9781788627481Supplemental Content