I am building a scoresheet that will score 2 teams of 5 players each as they compete to answer 20 questions. That means that I need 200 Entry boxes which I can create with a for loop. I am trying to bind them to the <Button-3> event but so far nothing I have tried works and googling for help has lead me nowhere. I want to create a Toplevel window that raises via the <Button-3> event so I can select scores that meet the rules of scoring. This will simplify the complicate scoring rules for the user.
I get the following Error at the Entry.bind lines;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
from tkinter import * #============================ Define Functions Here ============================ def call_top(): pass #============================ Define a Window Here ============================= root = Tk() root.title ( 'Quiz Score Sheet' ) # Window Size win_width = 1000 win_height = 500 #Center the Window on the Screen screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() x_coord = (screen_width / 2 ) - (win_width / 2 ) y_coord = (screen_height / 2 ) - (win_height / 2 ) root.geometry( "%dx%d+%d+%d" % (win_width, win_height, x_coord, y_coord)) #============================== Add Widgets Here =============================== # Add 3 Frames, 1 for each Team and 1 for Column Headers c_frm = Frame(root, width = 900 , height = 30 , bd = 3 , bg = '#d5dbd6' ) c_frm.pack() r_frm = Frame(root, width = 900 , height = 250 , bd = 3 , bg = '#ef5f5f' ) r_frm.pack() g_frm = Frame(root, width = 900 , height = 250 , bd = 3 , bg = '#5aeda8' ) g_frm.pack() # I need 20 column headers, 1 for each question and a Header for column 0 Label(c_frm, text = 'Contestants' , bd = 1 , relief = 'solid' , font = ( 'bold' , 10 ), width = 20 ).grid(row = 0 , column = 0 ) for r in range ( 21 , 1 , - 1 ): Label(c_frm, text = r - 1 , bd = 1 , relief = 'solid' , font = ( 'bold' , 10 ), width = 4 ).grid(row = 0 , column = r, padx = ( 2 , 0 )) # I need 10 row headers for Player Names on the Left side of the window PLAYERS = [ ( 'Player 1' , 1 ), ( 'Player 2' , 2 ), ( 'Player 3' , 3 ), ( 'Player 4' , 4 ), ( 'Player 5' , 5 ), ] for player, r in PLAYERS: Label(r_frm, text = player, bd = 1 , relief = 'solid' , font = ( 'bold' , 10 ), width = 20 ).grid(row = r, column = 0 , columnspan = 2 ) Label(g_frm, text = player, bd = 1 , relief = 'solid' , font = ( 'bold' , 10 ), width = 20 ).grid(row = r, column = 0 , columnspan = 2 ) # Now add the Entry boxes for Scoring Answers to the 20 Questions and Bind <Button-3> for r in range ( 1 , 6 ): # I need 5 rows numbered 1-5 for c in range ( 2 , 22 ): # I need 20 columns in each row numbered 1-20 Entry(r_frm, bd = 1 , relief = 'solid' , justify = 'center' , width = 4 ).grid(row = r, column = c) Entry.bind( '<Button-3>' , call_top) Entry(g_frm, bd = 1 , relief = 'solid' , justify = 'center' , width = 4 ).grid(row = r, column = c) Entry.bind( '<Button-3>' , call_top) #============================== Show it All Here =============================== root.mainloop() |
Error:Traceback (most recent call last):
File "/media/brad/PYTHON 3/MyStuff_py/for_loop_widgets.py", line 52, in <module>
Entry.bind('<Button-3>', call_top)
File "/usr/local/lib/python3.7/tkinter/__init__.py", line 1251, in bind
return self._bind(('bind', self._w), sequence, func, add)
AttributeError: 'str' object has no attribute '_bind'
Quite frankly I just don't get it. If I create each Entry box individually with 3 lines per Entry widget (Create, Grid and Bind) it works. If I # the Entry.bind('<Button-3>', call_top) lines it builds the score sheet but the Entry boxes are not bound to an event. Any direction you can point me to would be a big help. Thanks