Home > Net >  How can I remove the effect of the first Radiobutton when selecting different Radiobutton?
How can I remove the effect of the first Radiobutton when selecting different Radiobutton?

Time:11-28

I want to organize the GUI for my code, that has 2 sections. I'm organizing it by adding Radiobuttons, where when one is selected, it displays "Section 1" only. And when the other Radiobutton is selected, it should undisplay "Section 1", and display only "Section 2".

This is the idea, and it is all going well, except that if I change Radiobutton selection, it displays both sections.

How can I make it display only one at a time?

This is a sample code that can be run:

from tkinter import *

def main():
    root = Tk()
    app = Window(root)
    root.mainloop()


class Window():
    def __init__(self, master):
        self.master=master
        self.master.title("Test")
        self.master.geometry('1000x700')

        self.Radio=Label (self.master, text="Select Method", bg='Lavender')
        self.RadioFrame=LabelFrame(self.master, width=230, height=150, labelwidget=self.Radio, bg='lavender')
        self.RadioFrame.place(x=200, y=35)

        selection = IntVar()
        selection.set(0)
        r1 = Radiobutton(self.RadioFrame, text="Option 1", variable=selection, value=1, command=lambda: Clicked(selection.get()), bg='lavender').pack()
        r2 = Radiobutton(self.RadioFrame, text="Option 2", variable=selection, value=2, command=lambda: Clicked(selection.get()), bg='lavender').pack()

        def Clicked(variable):

            if selection.get() ==1:
                self.label_1=Label (self.master, text="label 1", bg='lavender')
                self.label_1Frame=LabelFrame(self.master, width=230, height=90, labelwidget= self.label_1, bg='lavender')
                self.label_1Frame.place(x=490, y=35)

            elif selection.get() ==2:
                self.label_2=Label (self.master, text="label_2", bg='lavender')
                self.label_2Frame=LabelFrame(self.master, width=230, height=60, labelwidget= self.label_2, bg='lavender')
                self.label_2Frame.place(x=490, y=340)

#root.mainloop()
if __name__=='__main__':
    main()

CodePudding user response:

You can use something like this:

try:
    self.any_widget.destroy()
except AttributeError:
    pass

To destroy any widget.
So, I have corrected your code:

from tkinter import *  

def main():    
    root = Tk()
    app = Window(root)
    root.mainloop()


class Window():
    def __init__(self, master):
        self.master=master
        self.master.title("Test")
        self.master.geometry('1000x700')

        self.Radio=Label (self.master, text="Select Method", bg='Lavender')
        self.RadioFrame=LabelFrame(self.master, width=230, height=150, labelwidget=self.Radio, bg='lavender')
        self.RadioFrame.place(x=200, y=35)

        selection = IntVar()
        selection.set(0)
        r1 = Radiobutton(self.RadioFrame, text="Option 1", variable=selection, value=1, command=lambda: Clicked(selection.get()), bg='lavender').pack()
        r2 = Radiobutton(self.RadioFrame, text="Option 2", variable=selection, value=2, command=lambda: Clicked(selection.get()), bg='lavender').pack()

        def Clicked(variable):
    
            if selection.get() ==1: 
                self.label_1=Label (self.master, text="label 1", bg='lavender')
                self.label_1Frame=LabelFrame(self.master, width=230, height=90, labelwidget= self.label_1, bg='lavender')
                self.label_1Frame.place(x=490, y=35)
                try:
                    self.label_2Frame.destroy()
                except AttributeError:
                    pass

            elif selection.get() ==2:
                self.label_2=Label (self.master, text="label_2", bg='lavender')
                self.label_2Frame=LabelFrame(self.master, width=230, height=60, labelwidget= self.label_2, bg='lavender')
                self.label_2Frame.place(x=490, y=340)
                try:
                    self.label_1Frame.destroy()
                except AttributeError:
                    pass

#root.mainloop()
if __name__=='__main__':
    main()

Try this one.

CodePudding user response:

You created new LabelFrame when radiobutton is clicked. You should create the two label frames first without showing them, and then show the one based on the selection of radiobuttons:

class Window():
    def __init__(self, master):
        self.master=master
        self.master.title("Test")
        self.master.geometry('1000x700')

        self.Radio=Label (self.master, text="Select Method", bg='Lavender')
        self.RadioFrame=LabelFrame(self.master, width=230, height=150, labelwidget=self.Radio, bg='lavender')
        self.RadioFrame.place(x=200, y=35)

        selection = IntVar()
        selection.set(0)
        r1 = Radiobutton(self.RadioFrame, text="Option 1", variable=selection, value=1, command=lambda: Clicked(selection.get()), bg='lavender').pack()
        r2 = Radiobutton(self.RadioFrame, text="Option 2", variable=selection, value=2, command=lambda: Clicked(selection.get()), bg='lavender').pack()

        # create the two label frames without showing them
        self.label_1=Label (self.master, text="label 1", bg='lavender')
        self.label_1Frame=LabelFrame(self.master, width=230, height=90, labelwidget= self.label_1, bg='lavender')

        self.label_2=Label (self.master, text="label_2", bg='lavender')
        self.label_2Frame=LabelFrame(self.master, width=230, height=60, labelwidget= self.label_2, bg='lavender')

        def Clicked(selected):

            if selected ==1:
                self.label_1Frame.place(x=490, y=35) # show label frame 1
                self.label_2Frame.place_forget() # hide label frame 2

            elif selected ==2:
                self.label_1Frame.place_forget() # hide label frame 1
                self.label_2Frame.place(x=490, y=340) # show label frame 2
  • Related