Python Forum
[Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets
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.. Huh Huh

from tkinter import *
from tkinter import ttk

#============================ Define Functions Here ============================
def call_top(event):

def create_pop(event):
    pop = Toplevel(root)
    pop.title('Quizzing Score')
    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
r_frm = Frame(root, width=900, height=250, bd=3, bg='#ef5f5f') #ef5f5f Light Red
g_frm = Frame(root, width=900, height=250, bd=3, bg='#5aeda8') #5aeda8 Light Green

#============================== 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
    ('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 ===============================

What didn't you like about the answer I provided to this question previously?

Instead of passing worthless event information you bind the event to a function and pass parameters. I think before I passed the team number, the player number, the question number, and a StingVar for getting or setting the value of the Entry widget that received the button click.

That will get all the information you need sent to create_pop(). The next step is binding the radio buttons back to the Event widget that was clicked. Your problem here is that you bound every entry widget to the same StringVar. You need to create a StringVar for each entry widget and pass that StringVar to create_pop()
I didn't truly understand all of that original response. With what you just shared I will go back to it and see how I can implement what you just told me.

Thank you for your help, please be patient as some things are just a little bit slower to sink in.

Brad Blush

I revisited your earlier response with the code example, I do appreciate the example, it was very helpful. When I run that code it does create a spreadsheet like window with 5 columns and 20 rows and a value in each cell. Each cell does respond to the <Button-3> event and prints to the shell 4 numbers that represent team, player, question and a cell value. That value though is not from the cell R-clicked on. The value is always from the cell in row 20 of the column that received the event. There is still no connection between the cell (Label or Entry) and the method the <Button-3> event runs.

In looking for something that would help me with this connection I was running print(id(event)) or print(type(event)) but only the type was consistent in it's return but with nothing of value to use to get back to the score sheet. It seems that there is no name or a unique identifier for a Label/Entry created in a loop. I will bet there is but I am just rookie enough to be ignorant of it. That will change.

Again, thank you for your assistance. I will keep working at it.


Possibly Related Threads…
Thread Author Replies Views Last Post
  .get() from generated Entry widgets in tkinter snakes 4 793 May-03-2021, 11:26 PM
Last Post: snakes
  [Tkinter] Images in Toplevel() finndude 4 1,382 Mar-09-2021, 09:39 AM
Last Post: finndude
Question Use radiobuttons to determine a total charge SalsaBeanDip 2 572 Nov-13-2020, 04:14 AM
Last Post: SalsaBeanDip
  Create image on a Toplevel using tkinter ViktorWong 3 3,511 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Default Values for radiobuttons xuraax 2 911 May-17-2020, 06:43 PM
Last Post: xuraax
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 2,326 Jan-23-2020, 09:00 PM
Last Post: HBH
  2 sets of radiobuttons Chuck_Norwich 4 1,177 Dec-02-2019, 07:31 PM
Last Post: Denni
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 1,266 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  [Tkinter] Returning Entry as a Label ashleyrfm94 2 1,775 Jul-22-2019, 04:47 PM
Last Post: Yoriz
  [Tkinter] Toplevel window and global widgets? KevinBrown 3 2,795 Apr-25-2019, 06:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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