Python Forum
[Tkinter] Binding Entry box to <Button-3> created in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Binding Entry box to <Button-3> created in for loop
#5
Here is some code that makes 100 entry widgets organized in 5 columns and all of them bound so clicking Button-3 calls a function.
import tkinter as tk

def button3_clicked(team, player, question, var):
    """This is a callback for a Button-3 event"""
    # Print out the team, player and question numbers as well as the answer.
    print(team, player, question, var.get())

def make_player(window, team, player):
    """I make 20 entries for a player"""
    boxes = []
    for i in range(20):
        var = tk.IntVar()
        entry = tk.Entry(window, textvariable=var)
        var.set(i+player*100)  # Set a value to verify button event works
        entry.grid(row=i, column=player)
        entry.bind('<Button-3>', \
                   lambda event, q= i: \
                   button3_clicked(team, player, q, var))
        boxes.append(var)
    return boxes

root = tk.Tk()
team1 = [make_player(root, 0, 0),
         make_player(root, 0, 1),
         make_player(root, 0, 2),
         make_player(root, 0, 3),
         make_player(root, 0, 4)]

tk.mainloop()
If you right click inside a box it calls the button3_clicked function and prints out the value in the Entry box. I used an IntVar, but if you want these to contain strings it is easy enough to use a StringVar. Entry's are a lot easier to work with using a variable than trying to work directly with the widget.
Reply


Messages In This Thread
RE: Binding Entry box to <Button-3> created in for loop - by deanhystad - Apr-22-2020, 03:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Key Binding scope angus1964 1 1,248 Jun-30-2022, 08:17 PM
Last Post: deanhystad
  kivy binding issue hammer 8 3,123 Nov-07-2021, 11:34 PM
Last Post: hammer
  [Tkinter] define entry via class on a loop drSlump 9 3,529 Oct-27-2021, 05:01 PM
Last Post: deanhystad
  [Tkinter] How to terminate a loop in python with button in GUI Joni_Engr 6 11,228 Sep-09-2021, 06:33 PM
Last Post: deanhystad
  [Tkinter] binding versus disable DPaul 6 6,901 May-05-2021, 05:17 PM
Last Post: DPaul
  [PyQt] Loop triggered by button help Purple0 1 2,359 May-17-2020, 02:57 AM
Last Post: deanhystad
  TkInter Binding Buttons ifigazsi 5 4,609 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Making text clickable with binding DT2000 10 5,231 Apr-02-2020, 10:11 PM
Last Post: DT2000
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,508 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 2,100 Nov-25-2019, 10:29 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020