New member of the stack here, looking for a little assistance with a problem I'm having with a small Python project of mine. I'm currently trying to create a GUI with the use of the tkinter-library, which is capable of adding (and later removing) new fields dynamically, depending on the user's wishes. I'm having an issue reading the state of a variable attached to a radio button widget, since I do not necessarily have direct access to the variable itself, only the widget.
To better explain my situation, I will include my code. Below is the base class for this project, detailing the creation of a window object: (note that I'm fairly new to Python, so please excuse any poor or suboptimal Python syntax!)
import tkinter as tk
class newWindow():
frames = [] # list of user-added frames
yPos = 1 # variable to count vertical positioning
def __init__(self):
self.window = tk.Tk()
mainFrame = tk.Frame(self.window)
fieldButton = tk.Button(mainFrame, text='+', command=self.addFrame)
fieldButton.grid(row=0, column=0)
checkButton = tk.Button(mainFrame, text='check', command=self.check)
checkButton.grid(row=0, column=1)
mainFrame.grid(row=0, column=0)
self.window.mainloop()
The "fieldButton" created in the main window is used to dynamically create fields containing a pair of radio buttons, each of which is linked to the same integer variable. The "checkButton" should check the state of the variable linked to the radio buttons for each field.
The following code-snip is the definition of the function used to create a new field. All of the elements are packed into a frame, and the entire frame is stored as a member of the class variable "frames". The integer variable "rbVar" is locally defined for each new frame, and is the one I would like to later reference:
def addFrame(self):
newFrame = tk.Frame(self.window)
rbVar = tk.IntVar(newFrame, 0)
rb1 = tk.Radiobutton(newFrame, text='RB_L', variable=rbVar, value=1)
rb1.grid(row=0, column=0)
rb2 = tk.Radiobutton(newFrame, text='RB_R', variable=rbVar, value=2)
rb2.grid(row=0, column=1)
newFrame.grid(row=self.yPos, sticky='ew')
self.frames.append(newFrame) # store frame for later reference
self.yPos += 1
Finally, the "check" function is supposed to simply retrieve the variable state belonging to each frame (here, it is simply printed out for the sake of example)
def check(self):
for unit in self.frames:
info = unit.winfo_children() # gather the widget info belonging to each frame
var = info[0]['variable'].get() # !PROBLEM! trying to retrieve variable
print(var)
if (__name__=='__main__'):
w = newWindow() # main program, calling new window instance
I have attempted using the get() and cget() methods, but the information returned from the winfo_children() function seems to be a string (which is incompatible with the method.) When checking the attributes of the first element in the list info[0], I have confirmed that it is indeed the radio button, which is parameterized with the variable PY_VAR# (where '#' is an incremented integer.) Any attempt to reference this variable directly (for example, with the getvar(name='PY_VAR#') method) results in the compiler telling me that a variable by that name does not exist, even though I am using the name of a variable read directly from the widget.
I haven't had any luck finding a solution to this, even in the tkinter documentation. Hopefully someone out there can give me a push in the right direction!
rbVar.get()to retrieve the value.