Apr-28-2020, 03:55 AM
I'm stuck once again. The project I have is to build a score card to receive input as 8-10 contestants complete in answering 20 questions per match. These quiz meets are a 2 day affair and the youth that are competing work hard to win competing in 8-10 matches per meet (long way to say a lot of scoring goes on).
I have been successful in creating the score card with the necessary 10 rows and 20 columns to record the scores for a match. In addition to that I have a Toplevel window that responds to the <Button-3> event and presents a window with 4 Radiobuttons.
Now for the problem, the Toplevel Radiobuttons respond to ALL the Entry widgets (which I plan to change to Labels as soon as I get this to work). I only want to respond to the Entry widget that received the <Button-3> event.
I have searched high and low for this but can't find an example to work from. You can see where I think the problem lies in the textvariable connection where I have tried using variables within the loop in the line creating the Entry boxes (lines 77 & 83). I have thought of creating a list and using that for the variable names but I think I will still miss the link.
How do I connect the Entry (or Label) to the selection on the Toplevel window? Love some ideas..
I have been successful in creating the score card with the necessary 10 rows and 20 columns to record the scores for a match. In addition to that I have a Toplevel window that responds to the <Button-3> event and presents a window with 4 Radiobuttons.
Now for the problem, the Toplevel Radiobuttons respond to ALL the Entry widgets (which I plan to change to Labels as soon as I get this to work). I only want to respond to the Entry widget that received the <Button-3> event.
I have searched high and low for this but can't find an example to work from. You can see where I think the problem lies in the textvariable connection where I have tried using variables within the loop in the line creating the Entry boxes (lines 77 & 83). I have thought of creating a list and using that for the variable names but I think I will still miss the link.
How do I connect the Entry (or Label) to the selection on the Toplevel window? Love some ideas..
from tkinter import *
from tkinter import ttk
#============================ Define Functions Here ============================
def call_top(event):
print(id(event))
print(type(event))
def create_pop(event):
pop = Toplevel(root)
pop.title('Quizzing Score')
pop.geometry('250x150')
correct = ttk.Radiobutton(pop, text='Correct Answer', variable=score, value=20)
wrong = ttk.Radiobutton(pop, text='Wrong Answer', variable=score, value='E')
bonus = ttk.Radiobutton(pop, text='Team Bonus', variable=score, value='B')
incorrect = ttk.Radiobutton(pop, text='Incorrect Bonus', variable=score, value='/')
correct.grid(row=0, column=0, padx=(20, 0), pady=(20,0), sticky='W')
wrong.grid(row=1, column=0, padx=(20, 0), sticky='W')
bonus.grid(row=2, column=0, padx=(20, 0), sticky='W')
incorrect.grid(row=3, column=0, padx=(20, 0), sticky='W')
#============================ Define a Window Here =============================
root = Tk()
root.title ('NWYM Bible Quizzing')
# 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 Frames Here ===============================
# Add 3 Frames, 1 for each Team, Red & Green, and 1 for Column Headers
c_frm = Frame(root, width=900, height=30, bd=3, bg='#000000') #000000 Black
c_frm.pack(pady=(10,5))
r_frm = Frame(root, width=900, height=250, bd=3, bg='#ef5f5f') #ef5f5f Light Red
r_frm.pack(pady=(5,5))
g_frm = Frame(root, width=900, height=250, bd=3, bg='#5aeda8') #5aeda8 Light Green
g_frm.pack(pady=(5,10))
#============================== Add Labels Here ===============================
# 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', 0),
('Player 2', 1),
('Player 3', 2),
('Player 4', 3),
('Player 5', 4),
]
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)
#============================== Add Entry Here ===============================
# Now add the Entry boxes for Scoring Answers to the 20 Questions and Bind <Button-3>
score = StringVar()
er_name = StringVar
eg_name = StringVar
r_var = StringVar
g_var = StringVar
for r in range(0, 5): # I need 5 rows numbered 0-4
for c in range(2, 22): # I need 20 columns in each row numbered 2-21
er_name = 'err' + str(r) + 'c' + str(c)
r_var = 'srr' + str(r) + 'c' + str(c)
er_name = Entry(r_frm, textvariable=score, bd=1, relief='solid', justify='center', width=4)
#er_name = Entry(r_frm, textvariable=r_var, bd=1, relief='solid', justify='center', width=4)
er_name.grid(row=r, column=c)
er_name.bind('<Button-3>', create_pop)
eg_name = 'egr' + str(r) + 'c' + str(c)
g_var = 'sgr' + str(r) + 'c' + str(c)
eg_name = Entry(g_frm, textvariable=score, bd=1, relief='solid', justify='center', width=4)
#eg_name = Entry(g_frm, textvariable=g_var, bd=1, relief='solid', justify='center', width=4)
eg_name.grid(row=r, column=c)
eg_name.bind('<Button-3>', create_pop)
#============================== Show it All Here ===============================
root.mainloop()

