How can I hide tkinter widgets based a radiobutton selection?












0















I am trying to write Python 3 code using tkinter to generate volume calculations based on entered data.



Currently, my code works, but what I would like to do is have the widgets for sphere disappear when rectangular prism is selected via the radio buttons, and vice versa, so that only the pertinent items are shown.



I've found some ideas but I'm having difficulties implementing them in my current setup. If someone could help, I'd very much appreciate it.



Code:



from tkinter import *
from tkinter import messagebox
from volumes import * #basic volume calculations
import sys

def calculate(): #assigns vars, calls volumes module

x = option.get()
if x == 1:
heightx = heighttxt.get()
widthx = widthtxt.get()
lengthx = lengthtxt.get()

height = float(heightx)
width = float(widthx)
length = float(lengthx)

volume= rp_volume(length, width, height)

messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

if x == 2:
radx = radtxt.get()
radius = float(radx)
volume = sp_volume(radius)
messagebox.showinfo('You selected Sphere', volume) #displays calculation result


window = Tk() #creates window ident

window.title("Volume Calculator")
window.geometry('450x300')

option = IntVar()
option.set(1)

Radiobutton(window, text="Rectangular Prism", variable=option, value=1).grid(column=1, row=1)
Radiobutton(window, text="Sphere", variable=option, value=2).grid(column=2, row=1)


heightlbl = Label(window, text="Enter the height: ", padx=5, pady=5) #creates ident labels

widthlbl = Label(window, text="Enter the width: ", padx=5, pady=5)

lengthlbl = Label(window, text="Enter the length: ", padx=5, pady=5)

radlbl = Label(window, text ="Or enter the radius of a sphere: ", padx=5, pady=5)


heighttxt = Entry(window,width=10) #creates entry boxes

widthtxt = Entry(window,width=10)

lengthtxt = Entry(window,width=10)

radtxt = Entry(window, width=10)


calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!

quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


heightlbl.grid(column=1, row=3) #assigns grid positions (preferred to pack for precise layout)
widthlbl.grid(column=1, row=4)
lengthlbl.grid(column=1, row=5)
radlbl.grid(column=1, row=6)
heighttxt.grid(column=2, row=3)
widthtxt.grid(column=2, row=4)
lengthtxt.grid(column=2, row=5)
radtxt.grid(column=2, row=6)
calcbtn.grid(column=2, row=7)
quitbtn.grid(column=2, row=8)



window.mainloop() #closes window mainloop









share|improve this question



























    0















    I am trying to write Python 3 code using tkinter to generate volume calculations based on entered data.



    Currently, my code works, but what I would like to do is have the widgets for sphere disappear when rectangular prism is selected via the radio buttons, and vice versa, so that only the pertinent items are shown.



    I've found some ideas but I'm having difficulties implementing them in my current setup. If someone could help, I'd very much appreciate it.



    Code:



    from tkinter import *
    from tkinter import messagebox
    from volumes import * #basic volume calculations
    import sys

    def calculate(): #assigns vars, calls volumes module

    x = option.get()
    if x == 1:
    heightx = heighttxt.get()
    widthx = widthtxt.get()
    lengthx = lengthtxt.get()

    height = float(heightx)
    width = float(widthx)
    length = float(lengthx)

    volume= rp_volume(length, width, height)

    messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

    if x == 2:
    radx = radtxt.get()
    radius = float(radx)
    volume = sp_volume(radius)
    messagebox.showinfo('You selected Sphere', volume) #displays calculation result


    window = Tk() #creates window ident

    window.title("Volume Calculator")
    window.geometry('450x300')

    option = IntVar()
    option.set(1)

    Radiobutton(window, text="Rectangular Prism", variable=option, value=1).grid(column=1, row=1)
    Radiobutton(window, text="Sphere", variable=option, value=2).grid(column=2, row=1)


    heightlbl = Label(window, text="Enter the height: ", padx=5, pady=5) #creates ident labels

    widthlbl = Label(window, text="Enter the width: ", padx=5, pady=5)

    lengthlbl = Label(window, text="Enter the length: ", padx=5, pady=5)

    radlbl = Label(window, text ="Or enter the radius of a sphere: ", padx=5, pady=5)


    heighttxt = Entry(window,width=10) #creates entry boxes

    widthtxt = Entry(window,width=10)

    lengthtxt = Entry(window,width=10)

    radtxt = Entry(window, width=10)


    calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!

    quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


    heightlbl.grid(column=1, row=3) #assigns grid positions (preferred to pack for precise layout)
    widthlbl.grid(column=1, row=4)
    lengthlbl.grid(column=1, row=5)
    radlbl.grid(column=1, row=6)
    heighttxt.grid(column=2, row=3)
    widthtxt.grid(column=2, row=4)
    lengthtxt.grid(column=2, row=5)
    radtxt.grid(column=2, row=6)
    calcbtn.grid(column=2, row=7)
    quitbtn.grid(column=2, row=8)



    window.mainloop() #closes window mainloop









    share|improve this question

























      0












      0








      0








      I am trying to write Python 3 code using tkinter to generate volume calculations based on entered data.



      Currently, my code works, but what I would like to do is have the widgets for sphere disappear when rectangular prism is selected via the radio buttons, and vice versa, so that only the pertinent items are shown.



      I've found some ideas but I'm having difficulties implementing them in my current setup. If someone could help, I'd very much appreciate it.



      Code:



      from tkinter import *
      from tkinter import messagebox
      from volumes import * #basic volume calculations
      import sys

      def calculate(): #assigns vars, calls volumes module

      x = option.get()
      if x == 1:
      heightx = heighttxt.get()
      widthx = widthtxt.get()
      lengthx = lengthtxt.get()

      height = float(heightx)
      width = float(widthx)
      length = float(lengthx)

      volume= rp_volume(length, width, height)

      messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

      if x == 2:
      radx = radtxt.get()
      radius = float(radx)
      volume = sp_volume(radius)
      messagebox.showinfo('You selected Sphere', volume) #displays calculation result


      window = Tk() #creates window ident

      window.title("Volume Calculator")
      window.geometry('450x300')

      option = IntVar()
      option.set(1)

      Radiobutton(window, text="Rectangular Prism", variable=option, value=1).grid(column=1, row=1)
      Radiobutton(window, text="Sphere", variable=option, value=2).grid(column=2, row=1)


      heightlbl = Label(window, text="Enter the height: ", padx=5, pady=5) #creates ident labels

      widthlbl = Label(window, text="Enter the width: ", padx=5, pady=5)

      lengthlbl = Label(window, text="Enter the length: ", padx=5, pady=5)

      radlbl = Label(window, text ="Or enter the radius of a sphere: ", padx=5, pady=5)


      heighttxt = Entry(window,width=10) #creates entry boxes

      widthtxt = Entry(window,width=10)

      lengthtxt = Entry(window,width=10)

      radtxt = Entry(window, width=10)


      calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!

      quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


      heightlbl.grid(column=1, row=3) #assigns grid positions (preferred to pack for precise layout)
      widthlbl.grid(column=1, row=4)
      lengthlbl.grid(column=1, row=5)
      radlbl.grid(column=1, row=6)
      heighttxt.grid(column=2, row=3)
      widthtxt.grid(column=2, row=4)
      lengthtxt.grid(column=2, row=5)
      radtxt.grid(column=2, row=6)
      calcbtn.grid(column=2, row=7)
      quitbtn.grid(column=2, row=8)



      window.mainloop() #closes window mainloop









      share|improve this question














      I am trying to write Python 3 code using tkinter to generate volume calculations based on entered data.



      Currently, my code works, but what I would like to do is have the widgets for sphere disappear when rectangular prism is selected via the radio buttons, and vice versa, so that only the pertinent items are shown.



      I've found some ideas but I'm having difficulties implementing them in my current setup. If someone could help, I'd very much appreciate it.



      Code:



      from tkinter import *
      from tkinter import messagebox
      from volumes import * #basic volume calculations
      import sys

      def calculate(): #assigns vars, calls volumes module

      x = option.get()
      if x == 1:
      heightx = heighttxt.get()
      widthx = widthtxt.get()
      lengthx = lengthtxt.get()

      height = float(heightx)
      width = float(widthx)
      length = float(lengthx)

      volume= rp_volume(length, width, height)

      messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

      if x == 2:
      radx = radtxt.get()
      radius = float(radx)
      volume = sp_volume(radius)
      messagebox.showinfo('You selected Sphere', volume) #displays calculation result


      window = Tk() #creates window ident

      window.title("Volume Calculator")
      window.geometry('450x300')

      option = IntVar()
      option.set(1)

      Radiobutton(window, text="Rectangular Prism", variable=option, value=1).grid(column=1, row=1)
      Radiobutton(window, text="Sphere", variable=option, value=2).grid(column=2, row=1)


      heightlbl = Label(window, text="Enter the height: ", padx=5, pady=5) #creates ident labels

      widthlbl = Label(window, text="Enter the width: ", padx=5, pady=5)

      lengthlbl = Label(window, text="Enter the length: ", padx=5, pady=5)

      radlbl = Label(window, text ="Or enter the radius of a sphere: ", padx=5, pady=5)


      heighttxt = Entry(window,width=10) #creates entry boxes

      widthtxt = Entry(window,width=10)

      lengthtxt = Entry(window,width=10)

      radtxt = Entry(window, width=10)


      calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!

      quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


      heightlbl.grid(column=1, row=3) #assigns grid positions (preferred to pack for precise layout)
      widthlbl.grid(column=1, row=4)
      lengthlbl.grid(column=1, row=5)
      radlbl.grid(column=1, row=6)
      heighttxt.grid(column=2, row=3)
      widthtxt.grid(column=2, row=4)
      lengthtxt.grid(column=2, row=5)
      radtxt.grid(column=2, row=6)
      calcbtn.grid(column=2, row=7)
      quitbtn.grid(column=2, row=8)



      window.mainloop() #closes window mainloop






      python tkinter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 10:27









      FeybinderFeybinder

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget the other. Here is the code. (Note that, I don't know anything about this volumes module, so you change that accordingly)



          from tkinter import *
          from tkinter import messagebox

          def calculate(): #assigns vars, calls volumes module
          x = option.get()
          if x == 1:
          heightx = heighttxt.get()
          widthx = widthtxt.get()
          lengthx = lengthtxt.get()

          height = float(heightx)
          width = float(widthx)
          length = float(lengthx)

          volume = length*width*height

          messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

          if x == 2:
          radx = radtxt.get()
          radius = float(radx)
          volume = 4/3*3.14*radius**3
          messagebox.showinfo('You selected Sphere', volume) #displays calculation result

          def recprism():
          sphere_frame.grid_forget()
          rec_frame.grid(row=1, column=0)

          def sphere():
          rec_frame.grid_forget()
          sphere_frame.grid(row=1, column=0)

          window = Tk() #creates window ident

          window.title("Volume Calculator")
          window.geometry('450x300')

          option = IntVar()

          Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
          Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)

          rec_frame = Frame(window)
          sphere_frame = Frame(window)

          heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
          widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
          lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)

          radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)

          heighttxt = Entry(rec_frame, width=10) #creates entry boxes
          widthtxt = Entry(rec_frame, width=10)
          lengthtxt = Entry(rec_frame, width=10)

          radtxt = Entry(sphere_frame, width=10)

          calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
          quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


          heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
          widthlbl.grid(column=0, row=1)
          lengthlbl.grid(column=0, row=2)
          radlbl.grid(column=0, row=0)
          heighttxt.grid(column=1, row=0)
          widthtxt.grid(column=1, row=1)
          lengthtxt.grid(column=1, row=2)
          radtxt.grid(column=1, row=0)

          calcbtn.grid(column=1, row=1)
          quitbtn.grid(column=1, row=2)

          window.mainloop()


          enter image description here






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390947%2fhow-can-i-hide-tkinter-widgets-based-a-radiobutton-selection%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget the other. Here is the code. (Note that, I don't know anything about this volumes module, so you change that accordingly)



            from tkinter import *
            from tkinter import messagebox

            def calculate(): #assigns vars, calls volumes module
            x = option.get()
            if x == 1:
            heightx = heighttxt.get()
            widthx = widthtxt.get()
            lengthx = lengthtxt.get()

            height = float(heightx)
            width = float(widthx)
            length = float(lengthx)

            volume = length*width*height

            messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

            if x == 2:
            radx = radtxt.get()
            radius = float(radx)
            volume = 4/3*3.14*radius**3
            messagebox.showinfo('You selected Sphere', volume) #displays calculation result

            def recprism():
            sphere_frame.grid_forget()
            rec_frame.grid(row=1, column=0)

            def sphere():
            rec_frame.grid_forget()
            sphere_frame.grid(row=1, column=0)

            window = Tk() #creates window ident

            window.title("Volume Calculator")
            window.geometry('450x300')

            option = IntVar()

            Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
            Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)

            rec_frame = Frame(window)
            sphere_frame = Frame(window)

            heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
            widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
            lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)

            radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)

            heighttxt = Entry(rec_frame, width=10) #creates entry boxes
            widthtxt = Entry(rec_frame, width=10)
            lengthtxt = Entry(rec_frame, width=10)

            radtxt = Entry(sphere_frame, width=10)

            calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
            quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


            heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
            widthlbl.grid(column=0, row=1)
            lengthlbl.grid(column=0, row=2)
            radlbl.grid(column=0, row=0)
            heighttxt.grid(column=1, row=0)
            widthtxt.grid(column=1, row=1)
            lengthtxt.grid(column=1, row=2)
            radtxt.grid(column=1, row=0)

            calcbtn.grid(column=1, row=1)
            quitbtn.grid(column=1, row=2)

            window.mainloop()


            enter image description here






            share|improve this answer




























              0














              To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget the other. Here is the code. (Note that, I don't know anything about this volumes module, so you change that accordingly)



              from tkinter import *
              from tkinter import messagebox

              def calculate(): #assigns vars, calls volumes module
              x = option.get()
              if x == 1:
              heightx = heighttxt.get()
              widthx = widthtxt.get()
              lengthx = lengthtxt.get()

              height = float(heightx)
              width = float(widthx)
              length = float(lengthx)

              volume = length*width*height

              messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

              if x == 2:
              radx = radtxt.get()
              radius = float(radx)
              volume = 4/3*3.14*radius**3
              messagebox.showinfo('You selected Sphere', volume) #displays calculation result

              def recprism():
              sphere_frame.grid_forget()
              rec_frame.grid(row=1, column=0)

              def sphere():
              rec_frame.grid_forget()
              sphere_frame.grid(row=1, column=0)

              window = Tk() #creates window ident

              window.title("Volume Calculator")
              window.geometry('450x300')

              option = IntVar()

              Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
              Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)

              rec_frame = Frame(window)
              sphere_frame = Frame(window)

              heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
              widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
              lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)

              radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)

              heighttxt = Entry(rec_frame, width=10) #creates entry boxes
              widthtxt = Entry(rec_frame, width=10)
              lengthtxt = Entry(rec_frame, width=10)

              radtxt = Entry(sphere_frame, width=10)

              calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
              quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


              heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
              widthlbl.grid(column=0, row=1)
              lengthlbl.grid(column=0, row=2)
              radlbl.grid(column=0, row=0)
              heighttxt.grid(column=1, row=0)
              widthtxt.grid(column=1, row=1)
              lengthtxt.grid(column=1, row=2)
              radtxt.grid(column=1, row=0)

              calcbtn.grid(column=1, row=1)
              quitbtn.grid(column=1, row=2)

              window.mainloop()


              enter image description here






              share|improve this answer


























                0












                0








                0







                To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget the other. Here is the code. (Note that, I don't know anything about this volumes module, so you change that accordingly)



                from tkinter import *
                from tkinter import messagebox

                def calculate(): #assigns vars, calls volumes module
                x = option.get()
                if x == 1:
                heightx = heighttxt.get()
                widthx = widthtxt.get()
                lengthx = lengthtxt.get()

                height = float(heightx)
                width = float(widthx)
                length = float(lengthx)

                volume = length*width*height

                messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

                if x == 2:
                radx = radtxt.get()
                radius = float(radx)
                volume = 4/3*3.14*radius**3
                messagebox.showinfo('You selected Sphere', volume) #displays calculation result

                def recprism():
                sphere_frame.grid_forget()
                rec_frame.grid(row=1, column=0)

                def sphere():
                rec_frame.grid_forget()
                sphere_frame.grid(row=1, column=0)

                window = Tk() #creates window ident

                window.title("Volume Calculator")
                window.geometry('450x300')

                option = IntVar()

                Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
                Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)

                rec_frame = Frame(window)
                sphere_frame = Frame(window)

                heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
                widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
                lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)

                radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)

                heighttxt = Entry(rec_frame, width=10) #creates entry boxes
                widthtxt = Entry(rec_frame, width=10)
                lengthtxt = Entry(rec_frame, width=10)

                radtxt = Entry(sphere_frame, width=10)

                calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
                quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


                heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
                widthlbl.grid(column=0, row=1)
                lengthlbl.grid(column=0, row=2)
                radlbl.grid(column=0, row=0)
                heighttxt.grid(column=1, row=0)
                widthtxt.grid(column=1, row=1)
                lengthtxt.grid(column=1, row=2)
                radtxt.grid(column=1, row=0)

                calcbtn.grid(column=1, row=1)
                quitbtn.grid(column=1, row=2)

                window.mainloop()


                enter image description here






                share|improve this answer













                To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget the other. Here is the code. (Note that, I don't know anything about this volumes module, so you change that accordingly)



                from tkinter import *
                from tkinter import messagebox

                def calculate(): #assigns vars, calls volumes module
                x = option.get()
                if x == 1:
                heightx = heighttxt.get()
                widthx = widthtxt.get()
                lengthx = lengthtxt.get()

                height = float(heightx)
                width = float(widthx)
                length = float(lengthx)

                volume = length*width*height

                messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result

                if x == 2:
                radx = radtxt.get()
                radius = float(radx)
                volume = 4/3*3.14*radius**3
                messagebox.showinfo('You selected Sphere', volume) #displays calculation result

                def recprism():
                sphere_frame.grid_forget()
                rec_frame.grid(row=1, column=0)

                def sphere():
                rec_frame.grid_forget()
                sphere_frame.grid(row=1, column=0)

                window = Tk() #creates window ident

                window.title("Volume Calculator")
                window.geometry('450x300')

                option = IntVar()

                Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
                Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)

                rec_frame = Frame(window)
                sphere_frame = Frame(window)

                heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
                widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
                lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)

                radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)

                heighttxt = Entry(rec_frame, width=10) #creates entry boxes
                widthtxt = Entry(rec_frame, width=10)
                lengthtxt = Entry(rec_frame, width=10)

                radtxt = Entry(sphere_frame, width=10)

                calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
                quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin


                heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
                widthlbl.grid(column=0, row=1)
                lengthlbl.grid(column=0, row=2)
                radlbl.grid(column=0, row=0)
                heighttxt.grid(column=1, row=0)
                widthtxt.grid(column=1, row=1)
                lengthtxt.grid(column=1, row=2)
                radtxt.grid(column=1, row=0)

                calcbtn.grid(column=1, row=1)
                quitbtn.grid(column=1, row=2)

                window.mainloop()


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 11:44









                Miraj50Miraj50

                2,7701924




                2,7701924
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390947%2fhow-can-i-hide-tkinter-widgets-based-a-radiobutton-selection%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()