简体   繁体   中英

Python tkinter - How can I activate again under conditions disabled Radiobutton created through a class?

Goal: The Radiobuttons created by the class "Rename_input" (grp4,grp5,grp6) are disabled. When the user is clicking on "Yes" in the class "RadiobuttonGroup" -"grp3" (dict "rename_map"), I want that the Radiobutton created by the class "Rename_input" (grp4,grp5,grp6) are active again (state normal) .

I tried the following code but without success (def activate_rename(self)).

    nbofcalc=9
    calculations_label = 'Calculation type'
    calculations_map = {'External': 'uc_1',
                        'Internal 1': 'uc_2',
                        'Internal 2': 'uc_3',}
    
    languages_label = 'Language'
    languages_map = {'French': 'FR',
                     'English': 'EN' ,
                     'German': 'GER'}
    
    rename_label = 'Do you want to rename the downladed files  ? '         
    rename_map={'Yes': 'Yes',
                 'No': 'No'}
    
    rename_position1_label = 'Input in Position 1 ?'
    rename_position1_map={'Calc. Number': 'Calc_1',
                'Input 2': 'Input_2',
                'Input 3': 'Input_3'}
    
    class RadiobuttonGroup:
        def __init__(self, parent, label_text, button_map):
            self.frame = Frame(parent)
            self.frame.pack(side=TOP, anchor=NW)
            self.label = Label(self.frame, text=label_text + ' :')
            self.label.pack(side=LEFT)
            self.button_map = button_map
            self.click = None
            self.var = StringVar(value='_nothing_')
            for button_label in self.button_map:
                radiobutton = Radiobutton(self.frame,
                                          text=button_label,
                                          value=button_label,
                                          variable=self.var,
                                          command= lambda: [self.choose(), self.activate_rename()])
                radiobutton.pack(side=LEFT)
            self.var2 = StringVar()
            self.frame2=Label(self.frame, textvariable=self.var2, fg="red")
            self.frame2.pack()
    
        def choose(self):
            self.click = self.button_map[self.var.get()]
            print(self.click)
    
        def activate_rename(self):
            i_t=0
            fenetre.update()
            while i_t<nbofcalc:
                if grp3.click=="Yes":
                    grp4.button_map[grp4.var.get()].config(state=NORMAL)
                    grp5.button_map[grp5.var.get()].config(state=NORMAL)
                    grp6.button_map[grp6.var.get()].config(state=NORMAL)
                fenetre.update()
                i_t+=1
       
    
    class Rename_input:
        def __init__(self, parent, label_text, button_map):
            self.frame = Frame(parent)
            self.frame.pack(side=TOP, anchor=NW)
            self.offset = Label(self.frame, text="                      ")
            self.offset.pack(side=LEFT)
            self.label = Label(self.frame, text=label_text + ' :')
            self.label.pack(side=LEFT)
            self.button_map = button_map
            self.click = None
            self.var = StringVar(value='_nothing_')
            for button_label in self.button_map:
                radiobutton = Radiobutton(self.frame,
                                          text=button_label,
                                          value=button_label,
                                          variable=self.var,
                                          state = DISABLED,
                                          command=self.choose)
                radiobutton.pack(side=LEFT)
            self.var2 = StringVar()
            self.frame2=Label(self.frame, textvariable=self.var2, fg="red")
            self.frame2.pack()
    
        def choose(self):
            self.click = self.button_map[self.var.get()]
            print(self.click)
    
    grp1 = RadiobuttonGroup(fenetre, calculations_label, calculations_map)
    grp2 = RadiobuttonGroup(fenetre, languages_label, languages_map)
    grp3 = RadiobuttonGroup(fenetre, rename_label, rename_map)

    grp4 = Rename_input(fenetre, rename_position1_label, rename_position1_map)
    grp5= Rename_input(fenetre, rename_position1_label, rename_position1_map)
    grp6= Rename_input(fenetre, rename_position1_label, rename_position1_map)

Error that I get when I call the function activate_rename():

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Bat02\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "C:\Users\Bat02\Desktop\Python\Tkinter - GUI 3 - Interface to download Calc v2.py", line 89, in <lambda>
    command= lambda: [self.choose(), self.activate_rename()])
  File "C:\Users\Bat02\Desktop\Python\Tkinter - GUI 3 - Interface to download Calc v2.py", line 102, in activate_rename
    grp4.button_map["Input 2"].config(state=NORMAL)
AttributeError: 'str' object has no attribute 'config'

Thank you !

I found the solution.

I just put the radiobutton in a list and then change the state.

Here is my new code and I marked the new solution:

nbofcalc=9
calculations_label = 'Calculation type'
calculations_map = {'External': 'uc_1',
                    'Internal 1': 'uc_2',
                    'Internal 2': 'uc_3',}

languages_label = 'Language'
languages_map = {'French': 'FR',
                 'English': 'EN' ,
                 'German': 'GER'}

rename_label = 'Do you want to rename the downladed files  ? '         
rename_map={'Yes': 'Yes',
             'No': 'No'}

rename_position1_label = 'Input in Position 1 ?'
rename_position1_map={'Calc. Number': 'Calc_1',
            'Input 2': 'Input_2',
            'Input 3': 'Input_3'}

class RadiobuttonGroup:
    def __init__(self, parent, label_text, button_map):
        self.frame = Frame(parent)
        self.frame.pack(side=TOP, anchor=NW)
        self.label = Label(self.frame, text=label_text + ' :')
        self.label.pack(side=LEFT)
        self.button_map = button_map
        self.click = None
        self.var = StringVar(value='_nothing_')
        for button_label in self.button_map:
            radiobutton = Radiobutton(self.frame,
                                      text=button_label,
                                      value=button_label,
                                      variable=self.var,
                                      command= lambda: [self.choose(), self.activate_rename()])
            radiobutton.pack(side=LEFT)
        self.var2 = StringVar()
        self.frame2=Label(self.frame, textvariable=self.var2, fg="red")
        self.frame2.pack()

    def choose(self):
        self.click = self.button_map[self.var.get()]
        print(self.click)

    def activate_rename(self):
        i_t=0
        fenetre.update()
        while i_t<nbofcalc:
            if grp3.click=="Yes":
                grp4.radiobutton_list[i_t].config(state=NORMAL) #------Solution: I change the state directly through the list
                grp5.radiobutton_list[i_t].config(state=NORMAL) #------Solution: I change the state directly through the list
                grp6.radiobutton_list[i_t].config(state=NORMAL) #------Solution: I change the state directly through the list
            fenetre.update()
            i_t+=1
   

class Rename_input:
    def __init__(self, parent, label_text, button_map):
        self.frame = Frame(parent)
        self.frame.pack(side=TOP, anchor=NW)
        self.offset = Label(self.frame, text="                      ")
        self.offset.pack(side=LEFT)
        self.label = Label(self.frame, text=label_text + ' :')
        self.label.pack(side=LEFT)
        self.button_map = button_map
        self.radiobutton_list=[] #----------- I created the list for each time the class will be called. 
        self.click = None
        self.var = StringVar(value='_nothing_')
        for button_label in self.button_map:
            radiobutton = Radiobutton(self.frame,
                                      text=button_label,
                                      value=button_label,
                                      variable=self.var,
                                      state = DISABLED,
                                      command=self.choose)
            radiobutton.pack(side=LEFT)
            self.radiobutton_list.append(radionbutton) #------ Solution: append the list
        self.var2 = StringVar()
        self.frame2=Label(self.frame, textvariable=self.var2, fg="red")
        self.frame2.pack()

    def choose(self):
        self.click = self.button_map[self.var.get()]
        print(self.click)

grp1 = RadiobuttonGroup(fenetre, calculations_label, calculations_map)
grp2 = RadiobuttonGroup(fenetre, languages_label, languages_map)
grp3 = RadiobuttonGroup(fenetre, rename_label, rename_map)

grp4 = Rename_input(fenetre, rename_position1_label, rename_position1_map)
grp5= Rename_input(fenetre, rename_position1_label, rename_position1_map)
grp6= Rename_input(fenetre, rename_position1_label, rename_position1_map)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM