-1

My program creates different widgets depending on the selection from a radio button. Everything works great except I can't seem to clear the old widget if the other radio button is selected. The suggestion here: (https://stackoverflow.com/a/15995920/3924118) isn't working. Here's the relevant code.

From the main program:

root = Tk()
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
secondframe = ttk.Frame(mainframe)
secondframe.grid(column=4, row=3)
secondframe.columnconfigure(0, weight=1)
secondframe.columnconfigure(0, weight=1)

And then the function:

def pct_from_duration():
    """ Calculate needed pct from target duration"""
    tgt_dur_entry = ttk.Entry(mainframe, width=4, textvariable=tgt_dur_inp)
    tgt_dur_entry.grid(column=5, row=3, sticky=(W, E))
    for widget in secondframe.winfo_children():
        widget.destroy()
    ttk.Label(secondframe, textvariable=pct_bond_end).grid(column=1, row=1)
    ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=5, row=4, sticky=W)

FWIW, I get no error message, it just doesn't actually delete the widgets. This is all python 3.6.

nbro
  • 15,395
  • 32
  • 113
  • 196
Tom
  • 1,003
  • 2
  • 13
  • 25

1 Answers1

1

Maybe this will help...I'ts an example of your function and it is working(Python 3.5)

import tkinter as tk
from tkinter import *
from tkinter import ttk

class GUI:

    def __init__(self, master):

        def pct_from_duration():
            tgt_dur_entry = ttk.Entry(mainframe, width=4)
            tgt_dur_entry.grid(column=5, row=3, sticky=(W, E))
            for widget in secondframe.winfo_children():
                widget.destroy()
            l3 = ttk.Label(secondframe, text = 'Label').grid(column=1, row=1)
            b2 = ttk.Button(mainframe, text="Calculate").grid(column=5, row=4, sticky=W)

        self.master = master
        mainframe = ttk.Frame(master)
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)
        secondframe = ttk.Frame(mainframe)
        secondframe.grid(column=4, row=3)
        secondframe.columnconfigure(0, weight=1)
        secondframe.columnconfigure(0, weight=1)
        l = Label(secondframe, text = 'Child_mainframe')
        l.grid()
        l2 = Label(mainframe, text = 'Child_secondframe')
        l2.grid()
        r1 = Radiobutton(master, text="Radiobutton", value=1, command = pct_from_duration).grid()

root = Tk()
root.rowconfigure(0, weight = 1)
root.columnconfigure(1, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(0, weight=2)
my_gui = GUI(root)
root.mainloop()
kavko
  • 2,751
  • 13
  • 27
  • If I'm not missing something, the main thing you did differently was put the whole thing in a class. What advantage is doing it this way? – Tom May 19 '18 at 13:28