#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    66
    Rep Power
    2

    How to disable & enable tkinter buttons


    I am learning Python 3.3 and looking for guidance in the code example below. My goal is to learn how to disable and enable
    widget command buttons at the appropriate times. Right now I
    can't figure out how to change from enabled to disabled or the reverse. There are 2 buttons that I want to do a page-down
    and page-up operation with a long list in a listbox. When the
    end page is reached, the page-down button should be disabled.

    When paging backwards and page 1 is reached, the page-up
    button should be disabled. Page-up should be enabled, if it is
    not, when the page-down button is pressed.

    When paging forward and the last page is reached, the page-
    down button should be disabled. Page-down should be enabled,
    if it is not, when the page-up button is pressed.

    Also I would appreciate any advice on whether the code
    below should be more 'pythonic'.
    Thanks.
    Code:
    from tkinter import *
    root = Tk()
    
    root.geometry('300x300+20+20')
    
    global currentpage
    global pagecount
    currentpage = 1
    pagecount = 5
    
    v = StringVar()
    Label(root, textvariable=v).place(x = 20, y = 10)
    
    v.set(str(currentpage))
       
    def page_up():
        """
        decrement displayed page number,
        disable Btn1 button when count reaches 1
        """
        global currentpage, pagecount
    
        currentpage -= 1
        
        #following line not needed when Btn1 is disabled
        currentpage = max(currentpage, 1)
        
        v.set(str(currentpage))
        
        if currentpage == 1:
            print("Btn1 should be disabled here")
    
        #Btn2 should be enabled here if is not currently enabled
            
    def page_down():
        """
        increment displayed page number,
        disable Btn2 button when count reaches 5
        """
        global currentpage, pagecount
    
        currentpage += 1
    
        #following line not needed when Btn2 is disabled
        currentpage = min(currentpage, pagecount)
        
        if currentpage >= pagecount:
            print("Btn2 should be disabled here")
    
        #Btn1 should be enabled here if is not currently enabled   
    
        v.set(str(currentpage))
                  
    Btn1 = Button(text = "Page Up", padx = 16, pady = 8, command = page_up)
    Btn1.pack()
    Btn1.place(x = 60, y = 10)
    
    Btn2 = Button(text = "Page Down", padx = 8, pady = 6, command = page_down)
    Btn2.pack()
    Btn2.place(x = 60, y = 60)        
    
    root.mainloop()
    Oops. I guess I posted the code incorrectly and it came out all on one line. This was a copy and
    paste between two code tags in a prompt. I'll try posting the code without
    the pop-up prompt. OKAY, that worked.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    2
    Use the config method:

    <button name>.config(state=STATE)

    where STATE=
    The button state: NORMAL, ACTIVE or DISABLED. Default is NORMAL.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    66
    Rep Power
    2
    Thankyou rrashkin for that solution. There may be some who are
    interested in seeing this in code, so I am posting it here.
    Code:
    # shows how command buttons can be disabled and enabled 
    from tkinter import *
    root = Tk()
    
    root.geometry('300x300+20+20')
    
    global currentpage
    global pagecount
    currentpage = 1
    pagecount = 5
    
    v = StringVar()
    Label(root, textvariable=v).place(x = 20, y = 10)
    v.set(str(currentpage))
       
    def page_up():
        """
        subtract 1 from displayed number,
        disable Btn1 button when currentpage reaches 1
        """
        global currentpage, pagecount
    
        currentpage -= 1 
        
        if currentpage == 1:
            Btn1.config(state=DISABLED)
            
        if Btn2["state"] != "normal":
            Btn2.config(state=NORMAL)   # button to page down is enabled
    
        v.set(str(currentpage))
          
    def page_down():
        """
        add 1 to displayed number,
        disable Btn2 button when currentpage reaches pagecount
        """
        global currentpage, pagecount
    
        currentpage += 1
     
        if currentpage >= pagecount:
            Btn2.config(state=DISABLED)
            
        if Btn1["state"] != "normal":
            Btn1.config(state=NORMAL)   # button to page up is enabled
        
        v.set(str(currentpage))
                  
    Btn1 = Button(text = "Page Up", padx = 16, pady = 8, command = page_up)
    Btn1.pack()
    Btn1.place(x = 60, y = 10)
    Btn1.config(state=DISABLED)
    
    Btn2 = Button(text = "Page Down", padx = 8, pady = 6, command = page_down)
    Btn2.pack()
    Btn2.place(x = 60, y = 60)        
    
    root.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo