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
#1
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):
    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()
Reply


Messages In This Thread
Connect Toplevel Radiobuttons to root Label/Entry widgets - by iconit - Apr-28-2020, 03:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,377 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,925 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Toplevel window menator01 5 3,082 Apr-18-2022, 06:01 PM
Last Post: menator01
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,934 Jan-07-2022, 10:10 PM
Last Post: finndude
  .get() from generated Entry widgets in tkinter snakes 4 4,253 May-03-2021, 11:26 PM
Last Post: snakes
  [Tkinter] Images in Toplevel() finndude 4 4,333 Mar-09-2021, 09:39 AM
Last Post: finndude
Question Use radiobuttons to determine a total charge SalsaBeanDip 2 1,750 Nov-13-2020, 04:14 AM
Last Post: SalsaBeanDip
  Create image on a Toplevel using tkinter ViktorWong 3 7,868 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Default Values for radiobuttons xuraax 2 3,866 May-17-2020, 06:43 PM
Last Post: xuraax
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,483 Jan-23-2020, 09:00 PM
Last Post: HBH

Forum Jump:

User Panel Messages

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