简体   繁体   中英

Python Tkinter CheckButton config issue

I am trying to create a simple gui with multiple radio buttons and two checkboxs. I would like to change the state of one checkbox depending on the radio button that is selected. However, when I try to do this my code fails at the line

def OnParallelChanged(self):
...
...
    elif self.ParallelVariable.get() == 2:
        self.AdaptiveChkBox.config(state='normal')

with the error :

Traceback (most recent call last):
File "~/Desktop/main.py", line 206, in <module>
  app = configuration(None)
File "~/Desktop/main.py", line 61, in __init__
  self.initialize()
File "~/Desktop/main.py", line 101, in initialize
  self.OnParallelChanged()
File "~/Desktop/main.py", line 157, in OnParallelChanged
  self.AdaptiveChkBox.config(state='normal')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
  return getattr(self.tk, attr)
AttributeError: AdaptiveChkBox

If I comment this line everything is ok. I don't understand the issue. Any help is appreciated. Here is the whole code:

import Tkinter

class configuration(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.buildBtn = Tkinter.Button(self, text=u'Build!',
                                       command=self.OnBuildButtonClick)
        self.buildBtn.grid(column=1, row=5)

        self.runBtn = Tkinter.Button(self, text=u'Run!',
                                     command=self.OnRunButtonClick)
        self.runBtn.grid(column=2, row=5)

        self.parallelLabel = Tkinter.Label(self,
                                           text=u'Choose device:',
                                           anchor='w')
        self.parallelLabel.grid(column=0, row=0, columnspan=2, sticky='EW')

        self.solverLabel = Tkinter.Label(self,
                                         text=u'Choose Solver:',
                                         anchor="w")
        self.solverLabel.grid(column=2, row=0, columnspan=2, sticky='EW')

        self.parallelMods = [
            ("MPI", 1),
            ("OpenMp", 2),
            ("GPU", 3)
        ]

        self.ParallelVariable = Tkinter.IntVar()
        self.ParallelVariable.set(2)

        for text, mode in self.parallelMods:
            self.b = Tkinter.Radiobutton(self, text=text,
                                         variable=self.ParallelVariable,
                                         value=mode,
                                         command=self.OnParallelChanged)
            self.b.grid(column=0, row=mode, sticky='W')

        self.SolverVariable = Tkinter.IntVar()
        self.OnParallelChanged()

        self.LogVariable = Tkinter.IntVar()
        self.LogVariable.set(1)
        self.LogChkBox = Tkinter.Checkbutton(self,
                                             text='Log solvers',
                                             variable=self.LogVariable)
        self.LogChkBox.grid(column=4, row=1, sticky='W')

        self.AdaptiveVariable = Tkinter.IntVar()
        self.AdaptiveVariable.set(1)
        self.AdaptiveChkBox = Tkinter.Checkbutton(self,
                                                  text='Adaptive',
                                                  state='disabled',
                                                  variable=self.AdaptiveVariable)
        self.AdaptiveChkBox.grid(column=4, row=2, sticky='W')

        self.grid_columnconfigure(0, weight=1)
        self.resizable(True, False)
        self.update()
        self.geometry(self.geometry())
        self.bind("<Return>", self.OnReturnHit)

    def OnBuildButtonClick(self):
        self.buildBtn.config(state='disabled')
        self.runBtn.config(state='disabled')

    def OnRunButtonClick(self):
        self.buildBtn.config(state='disabled')
        self.runBtn.config(state='disabled')

    def OnReturnHit(self, event):
        self.OnRunButtonClick()

    def OnParallelChanged(self):
        if self.ParallelVariable.get() == 1:
            # self.AdaptiveChkBox.config(state="disabled")
            self.SolverVariable.set(2)
            self.solverMods = [
                ("AGMG", 1, 'disabled'),
                ("Trilinos", 2, 'normal'),
                ("Paralution", 3, 'disabled')
            ]
        elif self.ParallelVariable.get() == 2:
            self.AdaptiveChkBox.config(state='normal')
            self.SolverVariable.set(2)
            self.solverMods = [
                ("AGMG", 1, 'normal'),
                ("Trilinos", 2, 'normal'),
                ("Paralution", 3, 'disabled')
            ]
        elif self.ParallelVariable.get() == 3:
            # self.AdaptiveChkBox.config(state="disabled")
            self.SolverVariable.set(3)
            self.solverMods = [
                ("AGMG", 1, 'disabled'),
                ("Trilinos", 2, 'disabled'),
                ("Paralution", 3, 'normal')
            ]

        self.setSolverRadioButtons(solverMods=self.solverMods)

    def setSolverRadioButtons(self, solverMods):
        for text, mode, sState in solverMods:
            b = Tkinter.Radiobutton(self, text=text,
                                    variable=self.SolverVariable,
                                    value=mode,
                                    state=sState)
            b.grid(column=2, row=mode, sticky='W')

    def center(self):
        self.update_idletasks()
        width = self.winfo_width()
        frm_width = self.winfo_rootx() - self.winfo_x()
        win_width = width + 2 * frm_width
        height = self.winfo_height()
        title_bar_height = self.winfo_rooty() - self.winfo_y()
        win_height = height + title_bar_height + frm_width
        x = self.winfo_screenwidth() // 2 - win_width // 2
        y = self.winfo_screenheight() // 2 - win_height // 2
        self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
        if self.attributes('-alpha') == 0:
            self.attributes('-alpha', 1.0)
        self.deiconify()


if __name__ == "__main__":
    app = configuration(None)
    app.title('Configure GIA')
    app.center()
    app.mainloop()

你打电话self.OnParallelChanged()initialize ,但你设置self.AdaptiveChkBox ,只有几行

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