Question

I have a Entry space to import my data in Tkinter. I wish to active or deactive this space using a Checkbutton. When Checkbutton == 1, you can insert data, when Checkbutton == 0 the space became grey and you can not insert data

from Tkinter import *
import tkMessageBox


class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)

        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta, onvalue = 0, offvalue = 1)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)

if __name__=="__main__":
   d=MainWindow()
   d.mainloop()
Was it helpful?

Solution

You need to do two things:

  1. Make a function to change the state of the entrybox that will be called each time the checkbutton is clicked. It should look something like this:

    def switch(self):
        if self.CheckVar_theta.get():
            self.entry11.config(state=DISABLED)
        else:
            self.entry11.config(state=NORMAL)
    
  2. Assign the checkbutton's command parameter to that function:

    self.check = Checkbutton(self, ..., command=self.switch)
    

In the end, your code will look like this:

from Tkinter import *
import tkMessageBox


class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)

        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta,
                                 onvalue = 0, offvalue = 1, command=self.switch)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)

    def switch(self):
        if self.CheckVar_theta.get():
            self.entry11.config(state=DISABLED)
        else:
            self.entry11.config(state=NORMAL)

if __name__=="__main__":
   d=MainWindow()
   d.mainloop()

OTHER TIPS

I think this should work:

from Tkinter import *
import tkMessageBox


class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)

        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta, onvalue = 0, offvalue = 1)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)

        self.update()

        def update(self):
            if self.CheckVar_theta.get() == 1:
                self.entry11.config(state=NORMAL)
            else:
                self.entry11.config(state=DISABLED)
            self.master.after(100, self.update)

if __name__=="__main__":
   d=MainWindow()
   d.mainloop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top