简体   繁体   中英

Tkinter button disable not working

I'm new to Python and even newer to GUI programming.

I've got a button and two spinboxes that I want to disable after I click the start button. I've Googled something 5 different ways to disable a Tkinter button and none seem to work. Theoretically the spinboxes should be disabled the same way but I'm just not having any luck. Getting really frustrated with the whole GUI thing.

self.gps_com and self.arduino_com are the two spinboxes

As you can see I tried to use the update() for the button but it doesn't work. I've seen variations of the code that use disabled in all caps, first letter cap'd and different variations of quotes. This current syntax gives no warnings/errors from Spyder.

I thought it was going to be easy to find the answer to this question but I've been at it now for a few hours.

def OnButtonClickStart(self):
    self.labelVariable.set( self.entryVariable.get())
    self.entry.focus_set()
    self.entry.selection_range(0, Tkinter.END)
    self.button_start.config(state = 'DISABLED')
    self.button_start.update()
    self.gps_com.config(state = 'DISABLED')
    self.arduino_com.config(state = 'DISABLED')

Try this piece of code and see if the text updates & the buttons disable/re-enable as expected:

import tkinter as tk

class Window():

    def __init__(self, root):

        self.frame = tk.Frame(root)
        self.frame.grid()

        self.i = 0
        self.labelVar = tk.StringVar()
        self.labelVar.set("This is the first text: %d" %self.i) 

        self.label = tk.Label(self.frame, text = self.labelVar.get(), textvariable = self.labelVar)
        self.label.grid(column = 0, row = 0)

        self.button = tk.Button(self.frame, text = "Update", command = self.updateLabel)
        self.button.grid(column = 1, row = 0)

        self.enableButton = tk.Button(self.frame, text = "Enable Update Button", state = 'disabled', command = self.enable)
        self.enableButton.grid(column = 2, row = 0)

    def updateLabel(self):

        self.i += 1
        self.labelVar.set("This is the new text: %d" %self.i)
        self.button.config(state = 'disabled')
        self.enableButton.config(state = 'active')

    def enable(self):

        self.button.config(state = 'active')
        self.enableButton.config(state = 'disabled')

root = tk.Tk()
window = Window(root)
root.mainloop()

If this works than you are either a) using the wrong keywords ( 'disabled' , all lowercase, in Python 2.5/3.4 (I tested 3.4 last night)) or b) the function that you are trying to call is not being executed properly as tobias_k suggested.

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