【问题标题】:Python tkinter - repetitive class of Radiobutton, how to assign a function to only one class?Python tkinter - Radiobutton的重复类,如何将一个函数分配给一个类?
【发布时间】:2021-08-14 19:52:30
【问题描述】:

此应用程序使用户能够自动将数据从 XML 文件传输到结构化表格中的 Excel。我有这类单选按钮,使用了 2 次(grp1 和 grp2)。

仅当单击 grp2(而不是 grp1)的单选按钮之一时才应调用函数 activate_lift_order()。我该怎么做?

    #---- some import
    
        position_label='Orientation of the datas'
        position_map={'Vertical - Figure' : 'vertical',
                      'Horizontal - Letters' : 'horizontal',
                      'Per Default':'default'}
        order_label='Order of the lift: '
        order_map={'Yes' : 'Yes',
                      'No' : 'NO'}
        
        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 + ' :',width=20)
                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.fill_entries(), save_config(),self.error_listbox(),self.activate_order_lift()])
                    radiobutton.pack(side=LEFT)
        
            def choose(self):
                self.click = self.button_map[self.var.get()]
                if grp2.click!=None:
                    bouton_equalizer.configure(state=NORMAL)
                    boutonlaunch.configure(state=NORMAL)
                print(len(user_entries_order))
            
            def fill_entries(self):
                global wst
                global load_config
                entries_entered()
                if grp1.click=='horizontal':
                    for i in range(nbitem):
                        user_entries[i].delete(0,'end')
                        user_entries_2[i].delete(0,'end')
                        user_entries[i].insert(0,1)
                        user_entries_2[i].insert(0,get_column_letter(i+1))
                elif grp1.click=='vertical':
                    print('verti')
                    for i in range(nbitem):
                        user_entries[i].delete(0,'end')
                        user_entries_2[i].delete(0,'end')
                        user_entries_2[i].insert(0,'A')
                        user_entries[i].insert(0,i+1)
                elif grp1.click=='default':
                    for i in range(len(head_list)):
                        user_entries[i].delete(0,'end')
                        user_entries_2[i].delete(0,'end')
                        user_entries[i].insert(0,1)
                        user_entries_2[i].insert(0,get_column_letter(i+1))
                        listbox_list[i].set(head_list[i])
                        
                    
                else:
                    print('rien')
                    pass
                    
            def error_listbox(self):
                global wst
                global load_config
                entries_entered()
                re=0
                for re in range(h):
                    if listbox_list[re].get() in head_list:
                        error_list[re].configure(text='')
                    else:
                        error_list[re].configure(text='')
                        error_list[re].configure(text='Please select a option', fg='red')
                        
            def activate_order_lift(self):
                    if grp2.click=='Yes':
                        bouton_load_config_order.configure(state=NORMAL)
                        order_lift()
                    else:
                        bouton_load_config_order.configure(state=DISABLED)        
        #----rest of the code -----
        
        fenetre=Tk()
        fenetre.geometry("900x700")
        
        grp2=RadiobuttonGroup(fenetre,order_label,order_map)
        grp1=RadiobuttonGroup(fenetre,position_label,position_map)

def order_lift():
    global loaded_file
    cadre_head2=Frame(cadre_loop2)
    cadre_head2.pack()

    for x in range(len(loaded_file)):

        cadre2=Frame(cadre_loop2)
        cadre2.pack()
        
        value_inside_order = StringVar()
        value_inside_order.set(lift_name[x])
        listbox_order = OptionMenu(cadre2, value_inside_order, *lift_name)
        listbox_order.config(width=20)
        listbox_order.pack(side=LEFT)
        
        user_entries_order.append(value_inside_order)
    
        fenetre.mainloop()

谢谢!

【问题讨论】:

    标签: python class tkinter


    【解决方案1】:

    RadiobuttonGroup 创建一个子类,并将函数activate_order_lift 限制为仅限子类。

    1. 使用现有类创建 grp1(不使用 activate_order_lift 函数)
    2. 使用新的子类创建grp2,如下:
    class RadiobuttonGroupGroup2 (RadiobuttonGroup):
        def __init__(self, *args, **kwargs):
            # call the __init__ of the main class RadiobuttonGroup
            # using the exact same values
            # using this method ensures you will have no need to change
            # this __init__ if you add parameters to the main class
            super().__init__(*args, **kwargs)
            
        def activate_order_lift(self):
            # do your function magic here
            pass
    

    Obligatory w3 link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-19
      • 2018-03-19
      • 2019-01-07
      • 2018-11-05
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多