#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2

    Does Widget Have Focus - How to Test


    How can I test for the tkinter widget that currently has
    focus? In the code below, using focus_get() returns a decimal.
    What is the correct way to use this method?
    Code:
    from tkinter import *
    
    root = Tk()
    root.geometry('400x300+20+20')
    
    def focus_on_lbox1():
        listbox1.focus_set()
        lblVar.set(listbox1.focus_get())
    
    def focus_on_lbox2():
        listbox2.focus_set()
        lblVar.set(listbox2.focus_get())
    
    lblVar = StringVar()
    lblMsg = Label(root, textvariable=lblVar)
    lblMsg.pack(side=TOP, anchor=NW)
        
    Label(root, text="Listbox #1").place(x=70, y=30)
    Label(root, text="Listbox #2").place(x=240, y=30)
    
    listbox1 = Listbox(root)
    listbox1.pack()
    listbox1.config(width=25, height=6)
    listbox1.place(x=30, y=50)
    
    listbox2 = Listbox(root)
    listbox2.pack()
    listbox2.config(width=25, height=6)
    listbox2.place(x=200, y=50)
    
    Btn1 = Button(padx = 8, pady = 6, text = "Move Focus to LBox #1",
                  command=focus_on_lbox1)
    Btn1.pack()
    Btn1.place(x = 30, y = 200)
    
    Btn2 = Button(padx = 10, pady = 6, text = "Move Focus to LBox #2",
                  command=focus_on_lbox2)
    Btn2.pack()
    Btn2.place(x = 200, y = 200)
    
    for items in ["1   First choice", "2   Second choice", "3   Third choice"]:
        listbox1.insert(END, items)
    
    for items in ["4   Fourth choice", "5   Fifth choice", "6   Sixth choice"]:
        listbox2.insert(END, items)
    
    root.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    focus_get() returns the Tkinter ID/instance of the widget.
    Code:
    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    def focus_on_lbox():
        if listbox1==listbox1.focus_get():
            lblVar.set("listbox1 had focus")
            listbox2.focus_set()
    
        elif listbox2==listbox2.focus_get():
            lblVar.set("listbox2 had focus")
            listbox1.focus_set()
    
    root = tk.Tk()
    root.geometry("500x250+10+10")
    lblVar = tk.StringVar()
    lblMsg = tk.Label(root, textvariable=lblVar)
    lblMsg.pack(side=tk.TOP, anchor=tk.NW)
        
    tk.Label(root, text="Listbox #1").place(x=70, y=30)
    tk.Label(root, text="Listbox #2").place(x=240, y=30)
    
    listbox1 = tk.Listbox(root)
    listbox1.pack()
    listbox1.config(width=25, height=6)
    listbox1.place(x=30, y=50)
    
    listbox2 = tk.Listbox(root)
    listbox2.pack()
    listbox2.config(width=25, height=6)
    listbox2.place(x=200, y=50)
    listbox1.focus_set()
    print listbox1, listbox2
    
    Btn1 = tk.Button(padx = 8, pady = 6, text = "Get and Set Focus",
                  command=focus_on_lbox)
    Btn1.pack()
    Btn1.place(x = 30, y = 200)
    
    #Btn2 = Button(padx = 10, pady = 6, text = "Move Focus to LBox #2",
    #              command=focus_on_lbox)
    #Btn2.pack()
    #Btn2.place(x = 200, y = 200)
    
    for items in ["1   First choice", "2   Second choice", "3   Third choice"]:
        listbox1.insert(tk.END, items)
    
    for items in ["4   Fourth choice", "5   Fifth choice", "6   Sixth choice"]:
        listbox2.insert(tk.END, items)
    
    root.mainloop()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    dwblas, your demonstration of comparing widget_name to
    widget_name.focus_get() to test whether or not the widget
    has focus, is the information I needed to know.
    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo