Acerox41
Acerox41

Reputation: 1

Allow Radiobuttonsto respond only once usingTkinter()

I´ve created two Radiobuttons labeled by "Yes" and "No" in one Tab. If "Yes" is selected a new Tab is created. If "No" is selected the created Tab is deleted.

It works fine but the problem is, that the Radiobuttons are responding each time if I click on them, although they are already selected. So for example I can click on "Yes" several times and a new Tab is created respectively. But I want to create a new Tab just once. How can I avoid this? Or is there another way to create and delete a new Tab dynamically?

Here is my code for creating the Radiobuttons in a Tab (implemented in a class):

self.checkVar = tk.BooleanVar(self.tab)
        self.checkY = tk.Radiobutton(self.tab,text="Yes",var=self.checkVar,value=True,comm=self.main.addTab)
        self.checkVar.set(0)
        self.checkY.grid(row=0,sticky="W")
        
        self.checkN = tk.Radiobutton(self.tab,text="No",var=self.checkVar,value=False,comm=self.main.addTab)
        self.checkN.grid(row=1,sticky="W")

Here is the code for creating respectively deleting the tabs:


def addTab():
#create new Tab
  if self.tab.checkVar.get() == True:
     self.tab_new = Tab_new(self.notebook)
# delete this tab again
  if self.tab.checkVar.get() == False:
     self.notebook.forget(2) 

Thanks for help!

Upvotes: 0

Views: 65

Answers (1)

fernando
fernando

Reputation: 99

I imagine it has to be a more proper way, but this should work I think.

x=0
def addTab():
     
    #create new Tab
      if self.tab.checkVar.get() == True and x == 0:
         self.tab_new = Tab_new(self.notebook)
         x = x+1
    
    # delete this tab again
      if self.tab.checkVar.get() == False:
         self.notebook.forget(2)
         x=0

Upvotes: 1

Related Questions