lundi 29 juin 2020

Changing Checkbox state using RadioButton input and Button command

I am learning Tkinter/python and still struggling to use them. I already search for my problem here and didn't find anything yet.

I am creating a user interface where:

  • I have several frames
  • two radio-button (protA and protB - left frame) defining if the user wants to do either Protocol A or Protocol B
  • some check-boxes defining steps in Protocol A or B to do (checkbutton1_check - middle frame)
  • one button (StatusButton - left frame) that is supposed to grey out the check-boxes of prot B if Radiobutton indicates Prot A and vice versa

My StatusButton's command is to grey out the checkboxes using the button.config(state='disabled') command. However, I received an error: AttributeError: 'NoneType' object has no attribute 'config'. I am sure checkboxes have this attribute. So maybe you can only define their state in their definition? In that case how can I change the state according to the radiobutton is it is in an other class?

Here is what I wrote so far:

from tkinter import *
from excelRead import *


class MainFrame(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        '''Frame on the middle'''
        self.middleFrame = MiddleFrame(self, bd=2, relief=GROOVE, padx=5, pady=5)
        self.middleFrame.grid(row=3, column=3)

        '''Frame on the Left'''
        self.leftFrame = LeftFrame(self, bd=2, relief=GROOVE, padx=5, pady=5)
        self.leftFrame.grid(row=3, column=2)

        self.StatusButton = Button(master=self.leftFrame, text="Send selection",
                                   command=lambda: self.leftframe_command()).grid(row=4, column=1)

    def leftframe_command(self):
        print(self.leftFrame.Var.get())

        selected = self.leftFrame.Var.get()
        self.middleFrame.checkbutton1_check.config(state='disabled' if selected == 0 else 'normal')

#  LEFT FRAME

class LeftFrame(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.Var = IntVar()
        self.protA = Radiobutton(self, text="Protocol A", variable=self.Var, value=0).grid(row=2, column=1)

        self.protB = Radiobutton(self, text="Protocol B", variable=self.Var, value=1).grid(row=3, column=1)


#  MIDDLE FRAME

class MiddleFrame(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.checkbutton1_var = IntVar()
        self.checkbutton1_check = Checkbutton(master=self, text="step 1",
                                            variable=checkbutton1_var).grid(row=5, column=2)
        
# other checkboxes are here


if __name__ == "__main__":
    # GUI root implementation
    root = Tk()
    root.title("Worklist generator")
    root.geometry("600x600")
    MainFrame(root).grid(row=0, column=0)

    root.mainloop()

I know it is a long code, and maybe not so easy to understand because not elegant! However, if someone knows why I have this attributeError, it would help me a lot.

Also, I tried to type self.checkbutton1_check = Checkbutton(master=self, text="step 1",variable=self.checkbutton1_var, state='normal' if self.parent.leftFrame.Var == 0 else 'disabled').grid(row=2, column=2)

but it tells me that 'MainFrame' object has no attribute 'leftFrame' which is false.

Thank you in advance for your time :)




Aucun commentaire:

Enregistrer un commentaire