As previously advocated (Best way to structure a tkinter application), I am trying to apply an object oriented approach to tkinter GUI structure.
I am trying to place widgets, like the Radiobutton below in Labelframes:
import tkinter as tk
root = tk.Tk()
root.title("GUI")
root.geometry('640x480+200+200')
lf0 = tk.LabelFrame(root, text='0 Label Frame', height=100, width=620)
lf0.grid(row=0, column=0, padx=10)
lf1 = tk.LabelFrame(root, text='1 Label Frame', height=100, width=620)
lf1.grid(row=1, column=0, padx=10)
lf2 = tk.LabelFrame(root, text='2 Label Frame')
lf2.grid(row=2, column=0, padx=10)
rb1 = tk.Radiobutton(lf2, text='set')
rb1.grid(padx=287, pady=29, sticky='ew')
root.mainloop()
The following results in a similar GUI layout, but I'm not able to place widgets within the label frames using this approach.
import tkinter as tk
class RadioBut(tk.Radiobutton):
def __init__(self, parent, txt, r, c, *args, **kwargs):
# def __init__(self, parent, loc, txt, r, c, *args, **kwargs):
tk.Radiobutton.__init__(self, parent, *args, **kwargs)
self.parent = parent
# self.loc = loc
rb = tk.Radiobutton(parent, text=txt)
# rb = tk.Radiobutton(self.loc, text=txt)
rb.grid(row=r, column=c, sticky='ew')
class LablFrm(tk.LabelFrame):
def __init__(self, parent, txt, r, c, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.label = tk.LabelFrame(parent, text=txt, height=100, width=620)
self.label.grid(row=r, column=c, padx=10)
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
parent.title("GUI")
parent.geometry('640x480+200+200')
self.widgets()
def widgets(self):
LablFrm(self.parent, '0 Label Frame', 0, 0)
LablFrm(self.parent, '1 Label Frame', 1, 0)
LablFrm(self.parent, '2 Label Frame', 2, 0)
RadioBut(self.parent, 'set', 3, 0)
# lf2 = LablFrm(self.parent, '2 Label Frame', 2, 0)
# rb2 = RadioBut(self.parent, lf2, 'set', 2, 0)
## rb2 = tk.Radiobutton(lf2, text='set')
## rb2.grid(row=0, column=0)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root)
root.mainloop()
I had hoped passing a location variable as in the commented lines above might work. Even the doubly commented approach failed. Something screwy with LablFrm definitions, among other things, I imagine...