-1

I have an need to have an initially Disabled button which is later enabled on an event. I have a rather large application that is working except I cannot get the state change to the button. Please don't tell me I need to completely resturcture the code. This is a strip out of all of the code leaving the salient structures I am using. ReadPort actually gets called from an 'after' but it simulated by the button press, here.

I have read all the helps on this subject and tried the answers. The Python statements and the errors each produces is in this complete application that fails in all attempts to change the button state. Please let me know how to fix this.

#!/usr/bin/python 3

from tkinter import *

def ReadPort():
  global VSM_Button

#  AttributeError: 'NoneType' object has no attribute 'config'
##  VSM_Button.config(state="normal")

#  AttributeError: 'NoneType' object has no attribute 'configure'
##  VSM_Button.configure(state="normal")

#  TypeError: 'NoneType' object does not support item assignment
##  VSM_Button['state'] = 'normal'

#  AttributeError: 'NoneType' object has no attribute 'configure'
##  VSM_Button.configure(state=NORMAL)

# ??? How do I set the button state to 'normal' ?

pass

class Application:
  def __init__(self, master):
    #global VSM_Button  # seems unnecessary.  Same errors in or out.
    frame = Frame(master)
    frame.pack()
    Button(frame, text='Press Me', width = 10, command=ReadPort).pack()
    VSM_Button = Button(frame, text='Disabled', width = 10, state = DISABLED).pack()

  pass  # end def __init__
pass  # end class Application

root = Tk()
root.wm_title('Button')
app = Application(master=root)
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • There are likely hundreds of questions and answers on this site related to the error "`NoneType' object has no attribute". – Bryan Oakley Nov 16 '16 at 00:53

1 Answers1

0
VSM_Button = Button(frame, text='Disabled', width = 10, state = DISABLED).pack()

You didn't assign the button itself to VSM_Button; you assigned the result of calling pack(), which is None. You need to do the pack on a separate line from the assignment.

The global VSM_Button statement is absolutely required. Without it, there is no possible access to the button outside of Application.__init__().

jasonharper
  • 9,450
  • 2
  • 18
  • 42
  • Splitting the definintion into 2 statements is necessary. I realized that yesterday. Still getting all these fine points or this (sometimes screwy) language into my head. So I separated the .grid statement off and have a Global for the name. I had put it at the top but that was not good. I just put it in the routine that creates the button and that tied it all together. Still getting used to the changes from other languages I have used. Thanks to all for sharing the helpful information here. We Python newbies appreciate it. – Micheal Morrow Nov 18 '16 at 23:42